0

I have json file something like this one.

{
    "SomethingA": {
        "SomethingB": {
            "SomethingC": {
                "C-property": "something",
                "C-property2": {}
            }
        }
   }
}

I want to add some new data top of the the "Something C" as "NEWSomethingC"

so It should be

{
    "SomethingA": {
        "SomethingB": {
            "NEWSomethingC": {
                "NEWC-property": "NEWsomething",
                "NEWC-property2": {}
            },
            "SomethingC": {
                    "C-property": "something",
                    "C-property2": {}
                }
        }
}
}

Okay, here is the problem. I can't add new value top of the keys. Always, NEWSomethingC is going to appear below the SomethingC.

The code I use for adding...

with open(credantials.init['config'], 'r+') as f:
        data = json.load(f)


        try:
            old_data = data['SomethingA'][SomethingB]
            append_data = data['SomethingA'][SomethingB]

            old_data = {NEWSomethingC :{'C-property':something, 'C-Property2':{}}}  

        except KeyError:
            print ('There is no key you want to search here')



        append_data.update(old_data)
        print(append_data)
        f.seek(0) 
        json.dump(data,f, indent=4)
        f.truncate()
  • It's a dict. It doesn't have an order. Top or bottom doesn't matter. If the order is important, use a list - or don't use json. – Aran-Fey Apr 07 '18 at 00:13
  • I know... I have searched a lot. However, This is for changing another program's config file. It is using json so I have to use json. The new data must be the top of the other keys because other program is reading it from top to bottom. Any other solution ? – WhiteRabbit Apr 07 '18 at 00:20
  • I imagine you'll need a custom json encoder, then. – Aran-Fey Apr 07 '18 at 00:21
  • What if we thought it as normal text ? Can we do something about it then ? I know its not pythonic way but makaroni code is also okay for me. – WhiteRabbit Apr 07 '18 at 00:28

2 Answers2

1

As already pointed out dictionaries in python are unorderd. Therefore we have to use OrderedDict As explained in this answer we can use the object_pairs_hook argument in json.loads() to load as OrderdDicts. Then we can add a new dictionary to our "OrderdJsonDictionary" and use the move_to_end function to move our added dictionary to the begin

with open(credantials.init['config'], 'r+') as f:
    data = json.load(f, object_pairs_hook=OrderedDict)
    new_data = {'newc':{
                    "NEWC-property": "NEWsomething",
                    "NEWC-property2": {}
                    }
                }
    data["SomethingA"]["SomethingB"].update(new_data)
    # last=False moves to the beginning 
    data["SomethingA"]["SomethingB"].move_to_end(list(new_data.keys())[0], last=False)  
    f.seek(0) 
    json.dump(data,f, indent=4)
    f.truncate()
optimalic
  • 511
  • 3
  • 17
  • Hello, I didn't know about OrderedDicts thank you it was really helpful. However, the code you wrote did not actually worked. I have large json file with many nested keys. It writes what I want but the bottom of the file after the original file Like this: https://paste.ee/p/irzAy – WhiteRabbit Apr 07 '18 at 20:34
  • Sorry, I'm don't quite understand what you mean. – optimalic Apr 07 '18 at 20:40
  • Yeah, seek(0) is important detail... I think it worked ! Thanks a lot. – WhiteRabbit Apr 07 '18 at 21:06
0

So what you would want to do is read the data, search for the data to the point where you wish to make an insertion. 1. Write that data to a new file 2. add your new insertion to the new file 3. add the rest of the file contents to the new file 4. delete the old file

So in order to write to file you would want to insert the following into your code.

outfile = open('file.json')
json.dump(data, outfile)
outfile.close()
Haris Nadeem
  • 1,322
  • 11
  • 24