0

I'm trying to update test.json file with a Python script. I only need to change the property of "plan2"->"2"->"rooms" to 3. Could you please help me with this? Thank you very much

test.json

{
"plan1" : [{
      "1": {
      "rooms":"2",
      "bathrooms":"1",
      "kitchens":"1"
      },

      "2": {
      "rooms":"1",
      "bathrooms":"1",
      "kitchens":"1"
      }
}],
"plan2":[{
      "1": {
      "rooms":"3",
      "bathrooms":"1",
      "kitchens":"1"
      },

      "2": {
      "rooms":"1",
      "bathrooms":"1",
      "kitchens":"1"
      }
}]
}

test.py

import json

with open("test.json", "r+") as jsonFile:
    data = json.load(jsonFile)

    # need to change the "plan2"->"apartments"->"2"->"rooms" to 3 

    jsonFile.seek(0)  # rewind
    json.dump(data, jsonFile)
    jsonFile.truncate()
1
  • Yes,it works! Thanks so much, But in my actual test.json there is another `"id" : plan2. I modified the question. Commented Feb 27, 2019 at 13:50

3 Answers 3

1
import json

with open("test.json", "r+") as jsonFile:
    data = json.load(jsonFile)

    # The change
    d['plan2'][0]['1']['rooms'] = 3

    jsonFile.seek(0)
    json.dump(data, jsonFile)
    jsonFile.truncate()
Sign up to request clarification or add additional context in comments.

8 Comments

It should be data['apartments'][0]['2']['rooms'] = 3 as mentioned: "apartments"->"2"->"rooms"
Sorry I forgot to add a part of the json file. Could you please modify the answer? Thanks so much
@brabbit640 Can you please update on which plan you want to change the rooms value.
@brabbit640 I think there is a problem with the json data. With your json there is duplicate id key. Maintain it as a json array.
@brabbit640 Updated the answer.
|
1

You are modifying an in-memory data structure, not the file itself. To be completely safe, do this in four steps:

  1. Read the file into memory
  2. Make the changes to your data structure
  3. Write the changes back to a new file
  4. After the write successfully completes, replace the old file with the new.

Putting it together,

import json
import tempfile

with open("test.json") as f:
    data = json.loa(f)

data['apartments'][0]['2'][rooms] = 3

with tempfile.NamedTemporaryFile(delete=False) as f:
    json.dump(data, f)
    os.rename(f.name, "test.json")

This assumes that your temporary directory is on the same file system as your JSON file, so that the rename operation is atomic. If that is not the case, use the dir argument to NamedTempfile to specify a directory that is on the same file system.

1 Comment

Really sorry I missed a part in my json file :( Could you please modify the answer. Thanks so much
0

Not a python answer, but if you have jq available on your system, you can use this:

jq '.plan2[]."2".rooms="3"' test.json

Demo on jqplay

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.