Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

json - How to append the list of dictionary to same list in python?

I'm having a JSON with nested values. I need to remove the key of the nested field and need to keep the values as plain JSON.

JSON(Structure of my JSON)

[
  {
"id":"101",
"name":"User1",
"place":{ 
    "city":"City1",
    "district":"District1",
    "state":"State1",
    "country":"Country1" 
    },
"access":[{"status":"available"}]
  }
]

I need to get the JSON output as:

Expected Output:

[
 {
 "id":"101",
 "name":"User1",
 "city":"City1",
 "district":"District1",
 "state":"State1",
 "country":"Country1" 
 "access":[{"status":"available"}]
 }
]

What i need is:

  1. I need to parse the JSON
  2. Get the Placefield out of the JSON
  3. Remove the key and brackets and append the values to existing

Python

for i in range(0,len(json_data)):
   place_data = json_data[i]['place']
   print(type(place_data)) #dict
   del place_data['place'] 

Any approach to get the expected output in python.?

question from:https://stackoverflow.com/questions/65839357/how-to-append-the-list-of-dictionary-to-same-list-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way to accomplish this could be by

for i in json_data:
    i.update(i.pop("place"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...