I have the following function in python parameter1: initial json parameter2: key parameter3: json to add
import simplejson as json
struct = json.loads(json_struct)
flag=0
for key in struct.keys():
if key == field_name:
data1 = json.loads(data)
if type(data1) == dict or type(data1) == list:
struct[key] = data1
flag=1
if flag == 0:
data1 = json.loads(data)
if type(data1) == dict or type(data1) == list:
struct[field_name] = data1
return json.dumps(struct)
If I call this
json_put_json('{"h":"a", "l": "p"}','b','[{"l":"o","m":"o"}]');
I get this:
{"h": "a", "b": [{"m": "o", "l": "o"}], "l": "p"}
but I want this:
{"h": "a", "l": "p", "b": [{"l": "o", "m": "o"}]}
I am losing the order. I want to conserve the order in the json. I would be happy if I get:
{"h": "a", "b": [{"l": "o", "m": "o"}], "l": "p"}
Thanks