Skip to main content

Form a JSON Object based on the key and values available in JSON Dataset

Below is the example to form a JSON Object based on the key and values available in the JSON dataset

Input Data with singleline json

{
        "id":123,
        "name":"sample",
        "lastname":"dataset",
        "attributes":[
            {
                "attributename":"item",
                "attributevalue":"27",
                "attribute_code":12234
            },
            {
                "attributename":"item2",
                "attributevalue":"47",
                "attribute_code":12334
            },
            {
                "attributename":"item1",
                "attributevalue":"37",
                "attribute_code":13234
            }]}

Below is the pycode ops

When we want to run a script for the data which is flowing. In this case the data will be stored in pycode_data variable, so use this variable for making changes in data using your script.

Responsedata=[]
new_data = pycode_data['attributes']
datalines = {}
for attribute in new_data:
    datalines[attribute["attributename"]] = attribute["attributevalue"]

pycode_data["datalines"] = datalines

In the above example we can see that we are running a script to make changes in the existing data so we are using pycode_data variable.

Output data after applying Python Pycode

{
    "id": 123, 
    "name": "sample",
    "lastname": "dataset", 
    "attributes":[
        {
            "attributename": "item", 
            "attributevalue": "27", 
            "attribute_code": 12234
        },
        {
            "attributename": "item2",
            "attributevalue": "47",
            "attribute_code": 12334
        },
        {
            "attributename": "item1",
            "attributevalue": "37",
            "attribute_code": 13234
        }],
    "datalines":{
        "item": "27", 
        "item2": "47",
        "item1": "37"}
    }