简体   繁体   中英

How to extract data from json and add additional values to the extracted values using python?

I want to parse the value from json response using python and assign additional value to the list

{ "form": [{ "box": [60,120,260,115], "text": "hello", "label": "question", "words": [{ "box": [90,190,160,215 ],"text": "hello"} ], "linking": [[0,13]],"id": 0 }]}

I am trying to parse the value and assign to a variable using python. What I am trying to achieve is: If the actual output is ([60,120,260,115],hello) I wanted to add few more values to the list: Thus expected output should be:

([60,120,260,120,260,115,60,115],hello)

try this:

tmp_json = { "form": [{ "box": [60,120,260,115], "text": "hello", "label": "question", "words": [{ "box": [90,190,160,215 ],"text": "hello"} ], "linking": [[0,13]],"id": 0 }]}
# Then do whatever you need to do with the list by accessing it as follows
# tmp_json["form"][0]["box"]

you can iterate through all elements of list here and if each item matches required condition extend the existing list with required values.

# Pseudocode
for item in data["form"]:
    # check each item's box attribute has all such elements i.e 60,120,260,115
    # AND item's text attribute has value "hello"
    # If matches then to add extra values to box list you can use <list>.extend([115, 120 etc])
    # e.g item["box"].extend([120, 115, 260])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM