I have a JSON data. I want to remove the ‘value’ key if only ‘key’ presents.
PFB sample data –
Here I want to remove ‘Job’ and ‘Mob No’ and keep rest of them as it is.
Actual Output –
{
"Name": "Nelson",
"Country": "India",
"Main": [
{
"Property": "House",
"details": [
{
"key": "No",
"value": "1"
},
{
"key": "Place",
"value": "KAR"
},
{
"key": "Job"
},
{
"key": "Mob No"
}
]
}
]
}
Expected Output –
{
"Name": "Nelson",
"Country": "India",
"Main": [
{
"Property": "House",
"details": [
{
"key": "No",
"value": "1"
},
{
"key": "Place",
"value": "KAR"
}
]
}
]
}
Kindly suggest a way to achieve this.
2
Answers
try iterating through the details list and ckeck each dictionary to see if it contains only the key and no value.
If so, remove that dictionary.
Like so:
if the structure of the json file is fixed, you can do like this.