0

I need to update (CRUD) a nested JSON file using Python. To be able to call python function(s)(to update/delete/create) entires and write it back to the json file.

Here is a sample file.

I am looking at the remap library but not sure if this will work.

    {
  "groups": [
    {
      "name": "group1",
      "properties": [
        {
          "name": "Test-Key-String",
          "value": {
            "type": "String",
            "encoding": "utf-8",
            "data": "value1"
          }
        },
        {
          "name": "Test-Key-Integer",
          "value": {
            "type": "Integer",
            "data": 1000
          }
        }
      ],
      "groups": [
        {
          "name": "group-child",
          "properties": [
            {
              "name": "Test-Key-String",
              "value": {
                "type": "String",
                "encoding": "utf-8",
                "data": "value1"
              }
            },
            {
              "name": "Test-Key-Integer",
              "value": {
                "type": "Integer",
                "data": 1000
              }
            }
          ]
        }
      ]
    },
    {
      "name": "group2",
      "properties": [
        {
          "name": "Test-Key2-String",
          "value": {
            "type": "String",
            "encoding": "utf-8",
            "data": "value2"
          }
        }
      ]
    }
  ]
}
1
  • There is not enough information. What is the problem you are having? Like Fhaab said, the existing json module lets you load JSON into a python dictionary. You can manipulate dictionaries as you see fit. Why do you feel like you need any additional library? Commented Jul 21, 2016 at 18:38

1 Answer 1

1

I feel like I'm missing something in your question. In any event, what I understand is that you want to read a json file, edit the data as a python object, then write it back out with the updated data?

Read the json file:

import json

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

That creates a dictionary (given the format you've given) that you can manipulate however you want. Assuming you want to write it out:

with open("data.json","w") as f:
  json.dump(data,f)
Sign up to request clarification or add additional context in comments.

6 Comments

Read/Write is fine, My challenge is to search for a key/value pair and update it with it with a new value? That is what I don't know how to do.
Well, then I think you should ask an entirely different question - how to search a dictionary for a key/value pair, and be clear - are you searching for where a key=search_key AND value=search_value? Or are you looking for value where search_key = something in particular? What happens when the search is "name" and you have a whole bunch of them in your data? Give the data above, say "this is my dictionary," and then say exactly what you'd like to accomplish programatically.
Thank you @Fhaab and mad-wombat for helping me better understand my problem. The Json file has UNIQUE group names. Like group1, group2. Each Unique group has properties that contain name and values. I need to search key/value pair where groups name = group1 and name="Test-Key-Integer" and data =1000 and update it to for example 2000. The combination of group name, properties name and data value is unique. Hope this helps. Should I modifiy the question to make it more clear?
Yes, I think you should clarify the question; also be clear if you want this to really be a generic search, or given that format of data you are looking for a function that specifically looks for group name "something1" where that group has name="something2" and data=something3. You may just need to write several functions to handle specific cases.
I will rewrite it. You comments was very very helpful. Should I make this as an answer since you actually make it very clear and start a new question? Or just mofify the existing questions. I though you should get some points for this :)
|

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.