0

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

  • possible duplicate of: http://stackoverflow.com/questions/6921699/can-i-get-json-to-load-into-an-ordereddict-in-python – 1.618 Apr 22 '15 at 21:46
  • your example desired output doesn't make sense. Where is `"l": "p"` coming from? – 1.618 Apr 22 '15 at 21:51

0 Answers0