Following is my sample json file:
{
"test": [{
"Location": "Singapore",
"Values": [{
"Name": "00",
"subvalues": [
"5782115e1",
"688ddd16e",
"3e91dc966",
"5add96256",
"10352cf0f",
"17f233d31",
"130c09135",
"2f49eb2a6",
"2ae1ad9e0",
"23fd76115"
]
},
{
"Name": "01",
"subvalues": [
"b43678dfe",
"202c7f508",
"73afcaf7c"
]
}
]
}]
}
I’m trying to remove from json file using the following list: ["130c09135", "2f49eb2a6", "5782115e1", "b43678dfe"]
end result:
{
"test": [{
"Location": "Singapore",
"Values": [{
"Name": "00",
"subvalues": [
"688ddd16e",
"3e91dc966",
"5add96256",
"10352cf0f",
"17f233d31",
"2ae1ad9e0",
"23fd76115"
]
},
{
"Name": "01",
"subvalues": [
"202c7f508",
"73afcaf7c"
]
}
]
}]
}
I know that using replace in text it would break the structure, new to json, any help would be appreciated.
2
Answers
You can use following code snippet:
Note: You must use ‘Subvalues’ with same writing in every instance. You can’t use ‘Subvalues’ and ‘subvalues’ in different instances…
Here is a generalised approach that does not rely on names of keys or depth. The only assumption is that if the dictionary contains any list comprised entirely of strings, it will be reconstructed excluding certain values – i.e., the EXCLUSIONS set
Output: