Home > Software engineering >  Update key values in file of json, maintain file integrity, Python 3
Update key values in file of json, maintain file integrity, Python 3

Time:09-16

I have file that has ~35k lines and I need to only change values for certain items in key base1, as below. I need to change id and title values and save the same file. Like, title becomes "title abc" and id becomes "789".

ex, before:

  "base1": {
    "base1base2": [
      {
        "id": "123",
        "title": "title xyz",
        "visibility": true
      }
    ],
    "title": "title xyz"
  }

After some research and trial-n-error, this below worked-ish. I did similar to below (without the indent) but my file size was reduced in half when I expected very little, if any, change in file size. Formatting was gone. I saw someone suggested adding the indent portion as below, so I did that, but the file size went up a bit more than I expected and tons of spaces and \n were added.

wmjf = 'webmap.json'

with open(wmjf, "r") as jf:
   data = json.load(jf)
   data['base1']['base1base2'][0]['id'] = '789'
   data['baseMap']['baseMapLayers'][0]['title'] = 'title abc'
   data['baseMap']['title'] = 'title abc'
    
with open(wmjf, "w") as jf:
  json.dump(json.dumps(data, indent=4), jf)

jf.close()

Can I get some help on what I am doing wrong here with respect to maintaining the file as is, it's integrity, when doing these very minor value changes. I wonder if the result of below is actually just fine for what I need (this reduces the size in half, no formatting, but I suspect it might work for republishing -- will try soon based on responses)

with open(wmjf, "w") as jf:
   json.dump(data, jf)

And, is this code good, correct, pythonic? I am not a developer, just need to do small scripts here and there over the years, so I appreciate being set straight on my attempts, thx! Also, I see some answers to similar questions with open mode r or w -- what does the mean?

CodePudding user response:

You don't need to call both json.dump() and json.dumps(). You're converting the data to JSON, then converting that JSON to another JSON string, so the newlines in the first JSON get converted to \n in the file.

Just call json.dump() and specify the indent there.

with open(wmjf, "w") as jf:
  json.dump(data, jf, indent=2)
  • Related