1

I have a very large json file with several nested keys. From whaat I've read so far, if you do:

x = json.loads(data)

Python will interpret it as a dictionary (correct me if I'm wrong). The fourth level of nesting in the json file contains several elements named by an ID number and all of them contain an element called children, something like this:

{"level1":
    {"level2":
        {"level3":
            {"ID1":
                {"children": [1,2,3,4,5]}
            }
            {"ID2":
                {"children": []}
            }
            {"ID3":
                {"children": [6,7,8,9,10]}
            }
      }
   }
}

What I need to do is to replace all items in all the "children" elements with nothing, meaning "children": [] if the ID number is in a list called new_ids and then convert it back to json. I've been reading on the subject for a few hours now but I haven't found anything similar to this to try to help myself.

I'm running Python 3.3.3. Any ideas are greatly appreciated!!

Thanks!!

EDIT

List:

new_ids=["ID1","ID3"]

Expected result:

{"level1":
    {"level2":
        {"level3":
            {"ID1":
                {"children": []}
            }
            {"ID2":
                {"children": []}
            }
            {"ID3":
                {"children": []}
            }
      }
   }
}
6
  • 2
    What part are you having trouble with? You obviously know how to load a JSON string to a Python object (yes, it is a dict). Are you having trouble changing the dict, making those changes based on another list, or converting back to JSON? Commented Jul 9, 2014 at 23:46
  • Good question, I don't know how to make the changes based on another list, the main issue is referring to the dict, I do know how to base myself on a list but not with a dict. Commented Jul 9, 2014 at 23:52
  • If your question is about modifying python dicts and you already know how to do the JSON stuff, I would suggest editing out the references to JSON to hone in on the real issue. Commented Jul 9, 2014 at 23:58
  • Show new_ids and expected result Commented Jul 9, 2014 at 23:59
  • Ok, I'm not sure why it is incorrect. Commented Jul 10, 2014 at 0:06

2 Answers 2

1

First of all, your JSON is invalid. I assume you want this:

{"level1":
    {"level2":
        {"level3":
            {
            "ID1":{"children": [1,2,3,4,5]},
            "ID2":{"children": []},
            "ID3":{"children": [6,7,8,9,10]}
            }
        }
    }
}

Now, load your data as a dictionary:

>>> with open('file', 'r') as f:
...     x = json.load(f)
... 
>>> x
{u'level1': {u'level2': {u'level3': {u'ID2': {u'children': []}, u'ID3': {u'children': [6, 7, 8, 9, 10]}, u'ID1': {u'children': [1, 2, 3, 4, 5]}}}}}

Now you can loop over the keys in x['level1']['level2']['level3'] and check whether they are in your new_ids.

>>> new_ids=["ID1","ID3"]
>>> for key in x['level1']['level2']['level3']:
...     if key in new_ids:
...         x['level1']['level2']['level3'][key]['children'] = []
... 
>>> x
{u'level1': {u'level2': {u'level3': {u'ID2': {u'children': []}, u'ID3': {u'children': []}, u'ID1': {u'children': []}}}}}

You can now write x back to a file like this:

with open('myfile', 'w') as f:
    f.write(json.dumps(x))

If your new_ids list is large, consider making it a set.

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome!!!! The for statement and the conversion back to json is just what I needed!! Thanks!!
just one thing, originally i was coding the json directly in my script this way: json_text="""this is the json stuff""" and it worked, but when i try to read it from a txt file i get this error TypeError: Input string must be text, not bytes
any ideas of why that happens?
@rodrigocf if you can isolate the problem and make a good minimal example you should open a new question for this. It's always hard to give a good answer in the comments.
You're right, just did: stackoverflow.com/questions/24703060/… :)
0

If you have simple dictionary like this

data_dict = {
    "level1": {
        "level2":{
            "level3":{
                "ID1":{"children": [1,2,3,4,5]},
                "ID2":{"children": [] },
                "ID3":{"children": [6,7,8,9,10]},
            }
        }
    }
}

than you need only this:

data_dict = {
    "level1": {
        "level2":{
            "level3":{
                "ID1":{"children": [1,2,3,4,5]},
                "ID2":{"children": [] },
                "ID3":{"children": [6,7,8,9,10]},
            }
        }
    }
}

new_ids=["ID1","ID3"]

for idx in new_ids:
    if idx in data_dict['level1']["level2"]["level3"]:
        data_dict['level1']["level2"]["level3"][idx]['children'] = []

print data_dict

'''    
{
    'level1': {
        'level2': {
            'level3': {
                'ID2': {'children': []}, 
                'ID3': {'children': []}, 
                'ID1': {'children': []}
             }
        }
    }
}
'''

but if you have more complicated dictionary

data_dict = {
    "level1a": {
        "level2a":{
            "level3a":{
                "ID2":{"children": [] },
                "ID3":{"children": [6,7,8,9,10]},
            }
        }
    },
    "level1b": {
        "level2b":{
            "level3b":{
                "ID1":{"children": [1,2,3,4,5]},
            }
        }
    }
}

new_ids =["ID1","ID3"]

for level1 in data_dict.values():
    for level2 in level1.values():
           for level3 in level2.values():
              for idx in new_ids:
                if idx in level3:
                    level3[idx]['children'] = []

print data_dict

'''
{
    'level1a': {
        'level2a': {
            'level3a': {
                'ID2': {'children': []}, 
                'ID3': {'children': []}
            }
        }
    },
    'level1b': {
        'level2b': {
            'level3b': {
                'ID1': {'children': []}
            }
        }
    }
} 
'''

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.