question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

enhancement: merge two json objects

See original GitHub issue

As offered here: https://stackoverflow.com/questions/50580415/karate-array-field-become-object by @ptrthomas Maybe we need a karate.append() and karate.merge() (for merging 2 JSON objects). Feel free to raise a feature request.

I would love to see a merge function. I have data driven tests where not all assertions in the test data files are mandatory, and I’d like to merge a “default” json with them, allowing the values in the file to override these defaults.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ptrthomascommented, May 14, 2019

@thunderstumpges done

Scenario: merge
    * def foo = { a: 1 }
    * def bar = karate.merge(foo, { b: 2 })
    * match bar == { a: 1, b: 2 }

Scenario: append
    * def foo = [{ a: 1 }]
    * def bar = karate.append(foo, { b: 2 })
    * match bar == [{ a: 1 }, { b: 2 }]
1reaction
thunderstumpgescommented, May 14, 2019

Wow, thanks! That has GOT to be the fastest turnaround on a feature request ever! 😃

Yes I have looked at contains and fuzzy matching. I actually do use fuzzy matching in a number of places. I ran into friction with contains, as the parts being asserted in the response are nested in fairly complex ways, and I didn’t want the test writers to have to replicate that nesting in their test data. I did also look into karate.map but with the “optional-ness” of the assertions, I didn’t see how to “optionally map” into the response structure.

I do have a working solution in place, and I think the above will help even more with this, so Thanks!

But for those curious, below is an example of my scenario. Test writers fill in test data in yaml files which drive the tests:

Test Yaml:

- input: some text
  response: '#regex ^response assertion$'
  maybeMatchString: match if specified in test
  maybeMatchArray: 
  - name: xxx
    value: yyy
- input: another test
  response: '#regex ^another response assertion$'
  # note, no optional match data, so don't assert on it

And when matching to the response, the nesting is more complex:

{
  "innerResponse": {
    "otherInnerObject" : {
      "textToMatchResponse" : "response value"
    }
  },
  "anotherMaybeObject" : {
    "nested" : {
      "maybeMatchString" : "match this if it was in the test"
    },
    "maybeMatchArray" : [
      {
        "name" : "match this if it was in the test",
        "value" : "same"
      }
    ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

merge two json objects · Issue #774 · karatelabs/karate - GitHub
enhancement : merge two json objects #774 ... Maybe we need a karate.append() and karate.merge() (for merging 2 JSON objects).
Read more >
Merge two json objects with jquery - Stack Overflow
I have been looking for a way to combine these two objects into one object. Thanks in advance. jquery · ajax · json...
Read more >
JavaScript merge JSON objects - techtutorialsx
In this tutorial we will learn how to merge two JSON objects using JavaScript. We will assume that we are starting from two...
Read more >
How to Merge Json Objects in Snowflake? - DWgeek.com
In this article, we will check how to merge two json objects in ... Note that, you can enhance this UDF to merge...
Read more >
Solved: Merge 2 Json files - Alteryx Community
Solved: I have 2 different JSON files which I want to merge as shown ... to be an array not 2 separate JSON...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found