convert list array to json

Question:

I’m working on taking a JSON feed and filtering out only the items I want from my list. I’m appending the items I’d like to keep to each list identifier. However, when I convert to JSON the output is incorrect. You can see the ACTUAL OUTPUT example below. The target output below is what I’m actually expecting. I’ve tried orienting the list with index and records, but no luck.

#TARGET OUTPUT
{
   "id":"1",
   "Name":"xxx",
   "Image":"https://xxx.xxx.png",
},
{
   "id":"2",
   "Name":"xx2",
   "Image":"https://xx2.xxx.png",
}



#ACTUAL OUTPUT
{
  "id": ["1","2",]
},
{
  "image":["https://xxx.xxx.png","https://xx2.xxx.png"] 
},
{
  "name":["xxx", "xx2"]
},

#CODE

# JSON feed
{
  "document": {
    "id": "1",
    "image": "https://xxx.xxx.png",
    "name": "xxx",
   },
 },
 {
  "document": {
    "id": "2",
    "image": "https://xx2.xxx.png",
    "name": "xx2",
   },
 },

# create list array
list = {'id':[], 'Name': [], 'Image': []}
links = {'id': [], 'Image': []}

# loop through and append items
def getData(hits):
    for item in filter(None, hits):
        item = item['document']
        list['id'].append(item['id'])
        links['id'].append(item['id'])
        links['Image'].append(item['image'])
        list['Image'].append(item['image'])
        list['Name'].append(item['name'])

    # get first page
    pageNum = 1
    data = getDataPerPage(pageNum)
    try:
        itemsNo = data['found']
        getData(data['hits'])

        while itemsNo > 24:
            itemsNo -= 24
            pageNum += 1
            data = getDataPerPage(pageNum)
            getData(data['hits'])
    
    except:
        print("broken")


    # save list to json
    with open('./output/data_chart.json', 'w') as f:
       f.write(json.dumps(list))
Asked By: askmeaquestion1234

||

Answers:

It’s not 100% clear what you have or what you want, but with a few assumptions (input is list of dict, desired output is list of dict):

json_obj = [
    {
        "document": {
            "id": "1",
            "image": "https://xxx.xxx.png",
            "name": "xxx",
        },
    },
    {
        "document": {
            "id": "2",
            "image": "https://xx2.xxx.png",
            "name": "xx2",
        },
    },
]

desired_output = [x["document"] for x in json_obj]
print(desired_output)
Answered By: jarmod

When you receive multiple JSON objects, those are in the form of a list (so between []). You could:

  1. covert JSON string to python dictionary using json.loads()
  2. filter using the dict
  3. dump dictionary into a JSON string using json.dumps()
input = """[
    {"document": 
        {"id": "1","image": "https://xxx.xxx.png","name": "xxx"}},
    {"document":
        {"id": "2","image": "https://xx2.xxx.png","name": "xx2"}}
    ]"""
input_dic = json.loads(input)
tmp = []
for item in input_dic:
    tmp.append(json.dumps(item["document"]))
output = json.dumps(tmp)
print(output)

Hope I got your question.

Answered By: Paolo Celada
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.