I’ve Json data as below mentioned
[
{
"id": 3,
"name": "ghi",
"gender": null,
"city": ""
},
{
"id": 5,
"name": "",
"gender": "",
"city": "es"
}
]
and I want to transform to data format shown below.
[
{
"field": "gender",
"remark": "testing",
"id": 3
},
{
"field": "city",
"remark": "testing",
"id": 3
},
{
"field": "name",
"remark": "testing",
"id": 5
},
{
"field": "gender",
"remark": "testing",
"id": 5
}
]
Here all keys whose values are null or "" are fetched and remark is added for those keys as testing with their respective id. I’m using executescript processor of nifi to transform this data based on the requirements.
I’ve tried with jolt but not able to find the solution so thought to move to executescript where I’ll use python. Can anyone provide script for this scenario?
2
Answers
You may iterate over each values, then over each pair and collect when the value is null or empty
First, you should parse the JSON into a list of dicts and then you can use list comprehension like this (assuming
values
has the input):