0

I have a json file that I need to modify. I've successfully done that, but when I push it to the json file, it appears at the bottom as if appended. I'm using seek(0) but that doesn't seem to work. Any suggestions?

f = open("C:\last_esri.json", "r")
data = json.load(f)
f.close()

for i in(data['fields']):
    for item in i:
        if i[item] == 'esriFieldTypeOID':
            i.update({'type' :'oid'})
        elif i[item] == 'esriFieldTypeString':
            i.update({'type' :'string'})
        elif i[item] == 'esriFieldTypeInteger':
            i.update({'type' :'integer'})
        elif i[item] == 'esriFieldTypeDouble':
            i.update({'type' :'double'})
        elif i[item] == 'esriFieldTypeDate':
            i.update({'type' :'date'})
 

f = open("C:\data\last_esri.json", 'w+')
f.seek(0)
f.write(json.dumps(data, indent=1))
f.close()         
Sarah
  • 1
  • Does this answer your question? [Items in JSON object are out of order using "json.dumps"?](https://stackoverflow.com/questions/10844064/items-in-json-object-are-out-of-order-using-json-dumps) – margusl Jul 21 '22 at 10:25

1 Answers1

0

That's not the idea of a dictionary; it has no order. If you do want it to be in a specific order, you should use a list.

EDIT: Even though it is not practical, if you really want it to be at the top, you could update your new dict, by the old one as shown:

x = {"a":3, "b":4, "c":8, "z":9}
y = {"e":2}

y.update(x)

print(y)

This puts the 'y' dictionary before the 'x' dictionary. The output of this example:

{'e': 2, 'a': 3, 'b': 4, 'c': 8, 'z': 9}
simon
  • 11
  • 4