Search code examples
pythonjsonapiappendjson-deserialization

Not getting the correct JSON format from pulled API data


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


Solution

  • 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)