JSON manipulation for substituting values python

Viewed 54

I have a scenario where I am substituting the values in a payload(data1) by manipulating a json payload(data2).

data2:

[
  {
    "eqid": 71430,
    "tags": [
      {
        "id": 135853,
        "content": "content1",
        "class_id": 13733,
        "class_name": "reEs"
      },
      {
        "id": 1358341,
        "content": "content2",
        "class_id": 13734447,
        "class_name": "reEp"
      },
      {
        "id": 135832561,
        "content": "content3",
        "class_id": 137342347,
        "class_name": "reEj"
      },
      {
        "id": 1358234561,
        "content": "content4",
        "class_id": 137123347,
        "class_name": "reEk"
      },
      {
        "id": 1355678561,
        "content": "content5",
        "class_id": 137432347,
        "class_name": "reEm"
      },
      {
        "id": 1352348561,
        "content": "content6",
        "class_id": 137786347,
        "class_name": "reEn"
      }
    ],
    "auth": false
  },
  {
    "eqid": 243582,
    "tags": [
      {
        "id": 1358456561,
        "content": "content1",
        "class_id": 137213347,
        "class_name": "reEl"
      },
      {
        "id": 13584567561,
        "content": "content2",
        "class_id": 13745347,
        "class_name": "reEt"
      },
      {
        "id": 1353218561,
        "content": "content3",
        "class_id": 137980347,
        "class_name": "reEf"
      },
      {
        "id": 13589758561,
        "content": "content4",
        "class_id": 1375678347,
        "class_name": "reEb"
      }
    ],
    "auth": false
  },
   {
    "eqid": 243672,
    "tags": [
      {
        "id": 1358456561,
        "content": "content1",
        "class_id": 137213347,
        "class_name": "reEl"
      },
      {
        "id": 13589758561,
        "content": "content4",
        "class_id": 1375678347,
        "class_name": "reEb"
      }
    ],
    "auth": false
  }
]

data 1 -

data1 =  {
  "data": [
    {
      "name": "app-pp",
      "ck_name": "App1",
      "eid": 71430,
      "Iar": "Osk",
      "sps": "Active",
      "tgs": "tobedonetages",
      "sid": "tobedoneservice",
      "last_checked": "19-05-2020"
    },
    {
      "name": "app-pq",
      "ck_name": "App2",
      "eid": 243582,
      "Iar": "Osk",
      "sps": "Active",
      "tgs": "tobedonetages",
      "sid": "tobedoneservice",
      "last_checked": "19-05-2020"
    }
  ]
}

Now here based on the condition that if eid of data1 is equal to eqid data2 then replace the value of payload data1 for this two key's tgs & sid with values from data2 of key's content (under tags) and auth.

What I have tried :

for tes in data2:

    tempmed = tes["eqid"]
    tempservice = tes["auth"]
    tempservicel = tes["tags"]
    for k in data1:
        templand= tempkey["name"]
        temphck= tempkey["ck_name"]
        tempevalid= tempkey["eid"]
        tempiaas= tempkey["Iar"]
        tempspc= tempkey["sps"]
        temptag= tempkey["tas"]
        tempserv= tempkey["sid"]
        templc = tempkey["last_checked"]
        if tempmed == tempevalid:
            tempserv = tempservice
    temptag = tempservicel
    data1.append({'name': templand, 'ck_name': temphck, 'eid': tempevalid, 'Iar': tempiaas, 'sps': tempspc, 'tgs': temptag, 'sid': tempserv, 'last_checked': templc})  

I am not sure what should be the approach to achieve this as the current approach of mine doesn't works as expected.

expected O/P :

{"data":[
   {
      "name":"app-pp",
      "ck_name":"App1",
      "eid":71430,
      "Iar":"Osk",
      "sps":"Active",
      "tgs":"content1,content2,content3,content4,content5,content6",
      "sid":"false",
      "last_checked":"19-05-2020"
   },
   {
      "name":"app-pq",
      "ck_name":"App2",
      "eid":243582,
      "Iar":"Osk",
      "sps":"Active",
      "tgs":"content1,content2,content3,content4",
      "sid":"false",
      "last_checked":"19-05-2020"
   }
]}

Any help would be great !

2 Answers

It is not optimal but it works. And it can be more readable for beginner.

for item1 in data1['data']:
    #print("item1['eid']: ", item1['eid'])

    for item2 in data2:

        if item1['eid'] == item2['eqid']:
            #print("item2['eqid']:", item2['eqid'])

            item1['sid'] = item2['auth']

            #c = []
            #for tag in item2['tags']:
            #    #print(tag['content'])
            #    c.append(tag['content'])
            #item1['tgs'] = ','.join(c)
            item1['tgs'] = ','.join(tag['content'] for tag in item2['tags'])

print(data1)

For bigger data it could be good first to use loop to create structure only with values content and auth from data2 and later use loop to replace it in data1. This way it would run less loops.


Full working example

data2 = [
  {
    "eqid": 71430,
    "tags": [
      {
        "id": 135853,
        "content": "content1",
        "class_id": 13733,
        "class_name": "reEs"
      },
      {
        "id": 1358341,
        "content": "content2",
        "class_id": 13734447,
        "class_name": "reEp"
      },
      {
        "id": 135832561,
        "content": "content3",
        "class_id": 137342347,
        "class_name": "reEj"
      },
      {
        "id": 1358234561,
        "content": "content4",
        "class_id": 137123347,
        "class_name": "reEk"
      },
      {
        "id": 1355678561,
        "content": "content5",
        "class_id": 137432347,
        "class_name": "reEm"
      },
      {
        "id": 1352348561,
        "content": "content6",
        "class_id": 137786347,
        "class_name": "reEn"
      }
    ],
    "auth": False
  },
  {
    "eqid": 243582,
    "tags": [
      {
        "id": 1358456561,
        "content": "content1",
        "class_id": 137213347,
        "class_name": "reEl"
      },
      {
        "id": 13584567561,
        "content": "content2",
        "class_id": 13745347,
        "class_name": "reEt"
      },
      {
        "id": 1353218561,
        "content": "content3",
        "class_id": 137980347,
        "class_name": "reEf"
      },
      {
        "id": 13589758561,
        "content": "content4",
        "class_id": 1375678347,
        "class_name": "reEb"
      }
    ],
    "auth": False
  },
   {
    "eqid": 243672,
    "tags": [
      {
        "id": 1358456561,
        "content": "content1",
        "class_id": 137213347,
        "class_name": "reEl"
      },
      {
        "id": 13589758561,
        "content": "content4",
        "class_id": 1375678347,
        "class_name": "reEb"
      }
    ],
    "auth": False
  }
]

data1 =  {
  "data": [
    {
      "name": "app-pp",
      "ck_name": "App1",
      "eid": 71430,
      "Iar": "Osk",
      "sps": "Active",
      "tgs": "tobedonetages",
      "sid": "tobedoneservice",
      "last_checked": "19-05-2020"
    },
    {
      "name": "app-pq",
      "ck_name": "App2",
      "eid": 243582,
      "Iar": "Osk",
      "sps": "Active",
      "tgs": "tobedonetages",
      "sid": "tobedoneservice",
      "last_checked": "19-05-2020"
    }
  ]
}

for item1 in data1['data']:
    print("item1['eid']: ", item1['eid'])
    for item2 in data2:
        if item1['eid'] == item2['eqid']:
            print("item2['eqid']:", item2['eqid'])
            item1['sid'] = item2['auth']
            c = []
            for tag in item2['tags']:
                print(tag['content'])
                c.append(tag['content'])
            c = ','.join(c)
            item1['tgs'] = c

print(data1)

try this

pool = {}
for d2 in data2:
    tags = d2["tags"]
    content = [i["content"] for i in tags]
    pool[d2["eqid"]] = [",".join(content), d2["auth"]]
#print(pool)
for i, d1 in enumerate(data1["data"]):
    if(d1["eid"] in pool):
        data1["data"][i]["tgs"] = pool[d1["eid"]][0]
        data1["data"][i]["sid"] = pool[d1["eid"]][1]

print(data1)
Related