1

I am trying to get this output into the correct JSON format. Unfortunately, that isn't really working out. All the data from an API pull is stored in a list which I then read in order to refine it and only get the data I need.

def refine_data():
    with open("api-pull.json", encoding='utf-8') as json_file:
        json_data = json.load(json_file)
        print('JSON loaded correctly')
        
        
        with open('REFINEDapi-pull.json', 'w', encoding='utf-8') as REFINED:
            for s in range(len(json_data)):
                
                if 'lat' in json_data[s] and 'lng' in json_data[s]: #if coordinates exist
                    
                    json.dump({
                        'name' : json_data[s]["name"],
                        'latitude' : json_data[s]["lat"],
                        'longitude' : json_data[s]["lng"],
                        'status' : json_data[s]["status"]["online"],
                        'address' : json_data[s]["address"],
                        'reward_scale' : json_data[s]["reward_scale"]
                        }, REFINED, ensure_ascii=False, indent=4)

Small sample of my current output:

Sample of what I am trying to accomplish:

Sample of what I am trying to accomplish

7
  • 1
    It is not clear from your question what you are trying to get or what isn't working for you. Oct 20, 2021 at 20:29
  • @coppereyecat I am sorry. Pretty new to StackOverflow. I added a link to a screenshot showing the incorrect JSON format I am getting.
    – DevJelle
    Oct 20, 2021 at 20:30
  • What format are you trying to get? what's the problem with your output? Oct 20, 2021 at 20:31
  • Although I suspect the issue is you are dumping one object at a time, not the entire array. Oct 20, 2021 at 20:32
  • I've added a picture of the result I am trying to get.
    – DevJelle
    Oct 20, 2021 at 20:34

1 Answer 1

2

What you should do is to append the dicts you create to a list and then json.dump() the single list to the file:

def refine_data():
    with open("api-pull.json", encoding='utf-8') as json_file:
        json_data = json.load(json_file)
        print('JSON loaded correctly')        
        
        with open('REFINEDapi-pull.json', 'w', encoding='utf-8') as REFINED:
            result = []   # start a list here
            for s in range(len(json_data)):
                if 'lat' in json_data[s] and 'lng' in json_data[s]: #if coordinates exist
                    result.append({
                        'name' : json_data[s]["name"],
                        'latitude' : json_data[s]["lat"],
                        'longitude' : json_data[s]["lng"],
                        'status' : json_data[s]["status"]["online"],
                        'address' : json_data[s]["address"],
                        'reward_scale' : json_data[s]["reward_scale"]
                        })

            json.dump(result, REFINED, ensure_ascii=False, indent=4)
1
  • This is exactly what I was looking for. Thank you very much! This makes a lot more sense.
    – DevJelle
    Oct 20, 2021 at 20:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.