avian2 / jsonmerge

Merge a series of JSON documents.
MIT License
214 stars 25 forks source link

Overwrite head json field with base if head field is empty("") #37

Closed asingh3886 closed 5 years ago

asingh3886 commented 5 years ago

Hi, Thanks for developing this amazing utility. I am facing a issue, suppose my head json contains a empty field and base has value then it should use base one.

Example:

base = { "foo": "4", "bar": [ "one" ], }

head = { "foo": "", "bar": [ "two" ], "baz": "Hello, world!" }

here it always takes foo "", but i want if head has empty(") field then it should consider from bases i.e foo: "4"

avian2 commented 5 years ago

jsonmerge does not treat empty strings in any special way. You will have to write your own strategy for that. One way to approach that would be to have a strategy for strings that keeps the base string, unless it's an empty string. Then in your schema use that strategy for fields where you want this behavior. Look into how Discard is implemented. Just change the check for undefined to a check for an empty string.