skip to Main Content

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


  1. You may iterate over each values, then over each pair and collect when the value is null or empty

    values = [
        {"id": 3, "name": "ghi", "gender": None, "city": ""},
        {"id": 5, "name": "", "gender": "", "city": "es"}
    ]
    result = []
    for item in values:
        for key, value in item.items():
            if not value and key != "id":
                result.append({"id": item["id"], "remark": "testing", "field": key})
    
    Login or Signup to reply.
  2. 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):

    import json
    
    jsondata = """[
      { "id": 3, "name": "ghi", "gender": null, "city": "" },
      { "id": 5, "name": "", "gender": "", "city": "es" }
    ]"""
    
    result = [
        {"id": item["id"], "remark": "testing", "field": key}
        for item in json.loads(jsondata)
        for key, value in item.items()
        if value in (None, "")
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search