Issue
I have a large nested JSON file that I need to remove a layer from, something similar to the following:
{
"children": [
{
"name": "FirstLayer 1",
"type": "Folder",
"children": [
{
"name": "ID12345",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
}
]
},
{
"name": "FirstLayer",
"type": "Folder",
"children": [
{
"name": "ID98765",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
}
]
}
]
}
There are a bunch of separate layers I need to remove-- they're all at the same indentation level, if that makes a difference.
In this example, I need to remove the layer(s) with the "name": "ID12345" and "name "ID98765" and everything at that level / layer.
The desired outcome would be like the following, with that layer removed:
{
"children": [
{
"name": "FirstLayer 1",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
},
{
"name": "FirstLayer",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
}
]
}
I can't use dict.pop since as far as I know, it pops out the key value pair based on the key name, not the value. In addition, it wouldn't pop out the related "type" and "children" key-value pairs underneath that go with it. Are there any other functions that would use the key-value instead? Or would this have to be an entirely custom function / looping job here?
Would like to accomplish this with Python but am open to other ways as well.
Solution
If you keep track of grandparents, parents and children you can promote children to parents via a comprehension. Note that this mutates your original data which is something you may or may not want to do.
Given:
data = {
"children": [
{
"name": "FirstLayer 1",
"type": "Folder",
"children": [
{
"name": "ID12345",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
}
]
},
{
"name": "FirstLayer",
"type": "Folder",
"children": [
{
"name": "ID98765",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
}
]
}
]
}
Then this:
for grand_parent in data["children"]:
grand_parent["children"] = [
child
for parent in grand_parent["children"]
for child in parent["children"]
]
import json
print(json.dumps(data, indent=4))
Should give you:
{
"children": [
{
"name": "FirstLayer 1",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
},
{
"name": "FirstLayer",
"type": "Folder",
"children": [
{
"key1": "abc",
"key3": "Float8"
},
{
"key2": "abc",
"key4": "Float8"
}
]
}
]
}
Answered By - JonSG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.