1

I have following code to merge values in my json:

from jsonmerge import merge

with open('env.json') as data_file:
    data = json.load(data_file)
    result2 = merge("", data.get('default_attributes'))
    result3 = merge(result2, data.get('normal_attributes'))
    result4 = merge(result3, data.get('override_attributes'))
    result5 = merge(result4, data.get('force_override_attributes'))
    > print result4, result5
    result6 = merge(result5, data.get('automatic_attributes'))
    cookbook_versions = {"cookbook_versions" : data.get('cookbook_versions')}
    result7 = merge(result6, cookbook_versions)

Now when I print result4, result5 I get :

result4 = {u'modmon': {u'env': u'dev'}, u'default': {u'env': u'developmen-jq'}, u'paypal': {u'artifact': u'%5BINTEGRATION%5D'}, u'windows': {u'password': u'Pib1StheK1N5'}, u'task_sched': {u'credentials': u'kX?rLQ4XN$q'}, u'seven_zip': {u'url': u'https://.io/artifactory/djcm-zip-local/djcm/chef/paypal/7z1514-x64.msi'}, u'7-zip': {u'home': u'%SYSTEMDRIVE%\7-zip'}}

result5 = None

which doesn't make sense to me as in result5 I'm merging result 4 which already has content in it then why does it come out null ?

2
  • I hope that this is a fake password... Commented Jul 27, 2016 at 16:47
  • yup removed it and is fake. :D Commented Jul 27, 2016 at 16:48

1 Answer 1

2

If data.get('force_override_attributes') is None then merge(result4, data.get('force_override_attributes')) is None

>>> a = {"a":10}
>>> b = merge(a, None)
>>> print b 
    None

What you can do is:

result5 = merge(result4, data.get('force_override_attributes') or {})

So even if it is an None the value of result4 will be retained.

or another option is to reverse the order, this should also work:

result5 = merge(data.get('force_override_attributes'), result4)
Sign up to request clarification or add additional context in comments.

1 Comment

why is that ? Shoudnt merge merge the two values and not overwrite non common values? any doc links to support this ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.