-2

I've two json objects, I 'm figuring out a way on how to merge them in python

y={"success":"true"}
x={"0":"740","1":"747","2":"883","3":"750","4":"769"}

I need the final result in the following manner

{"success":"true",
  "data":{
       "0":"740",
       "1":"747",
       "2":"883", 
       "3":"750", 
       "4":"769"
         }
              }

I am trying to append it, but its showing some error. Help me with the code in python.

Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43

3 Answers3

2

your input seem to be strings and not dictionaries. you need to convert them to dicts using json.loads:

import json

y = '{"success":"true"}'
x = '{"0":"740","1":"747","2":"883","3":"750","4":"769"}'

res = json.loads(y)
res['data'] = json.loads(x)

print(res)

if you need the output as string again, use json.dumps:

res_str = json.dumps(res)

if you insist on having the ouput sorted:

res_str = json.dumps(res, sort_keys=True)
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
  • Getting output as {"data": {"1": "747", "0": "740", "3": "750", "2": "883", "4": "769"}, "success": "true"}, why is 1 coming infront of 0? – Sandeep Rajamahendravarapu Jun 15 '17 at 08:52
  • 1
    dictionaries are unordered. see [this](https://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary) for example. – hiro protagonist Jun 15 '17 at 08:55
  • Because python dicts, by definition are not ordered. If you need to have ordered dict you can either sort it on keys or use something like [ordereddict](https://stackoverflow.com/questions/6921699/can-i-get-json-to-load-into-an-ordereddict-in-python) in which case the conversion becomes more complex of course. – SRC Jun 15 '17 at 08:56
  • @SandeepRajamahendravarapu: added a way how you can sort the output with `json.dumps`. but that will only concern the json string; not the dict itself. – hiro protagonist Jun 15 '17 at 08:57
1

You can simply do y["data"] = x

y={"success":"true"}
x={"0":"740","1":"747","2":"883","3":"750","4":"769"}

y["data"] = x
codeiscool
  • 119
  • 2
  • 7
0

I assume that they are strings and not python dicts. So here is what you can do

y='{"success":"true"}'
x='{"0":"740","1":"747","2":"883","3":"750","4":"769"}'

import json
dict1 = json.loads(x)
dict2 = json.loads(y)

dict2['data'] = dict1

result = json.dumps(dict2)
print result

The code above gives you this

{"data": {"1": "747", "0": "740", "3": "750", "2": "883", "4": "769"}, "success": "true"}

If you want to have the structure in the json string preserved you can look into this link. That will make the decoding a little bit more complex.

SRC
  • 2,123
  • 3
  • 31
  • 44
  • {"1": "747", "0": "740", "3": "750", "2": "883", "4": "769", "data": {"success": "true"}} wrong output! – Sandeep Rajamahendravarapu Jun 15 '17 at 08:55
  • Changed the code a bit to fit the output. I also mentioned a link to use ordered dicts to be be created directly from json string in my comment to other answer. Let me know if it helps. – SRC Jun 15 '17 at 09:55