[Answered ]-Python for loop not return multiple dict

2πŸ‘

βœ…

The assignment of jsoninfo is happening outside of the for-loop but the assignment of geoinfo is happening inside of it. You need to aggregate all of the geoloc.attrib values into a list and convert this to json at the end:

def xml_parser(request):
    infos = []

    for child in root.findall('GetAll'):
        for geoloc in child.iter('loc'):
            infos.append(geoloc.attrib)

    jsoninfo = json.dumps(infos, ensure_ascii=False)
    return HttpResponse(jsoninfo, content_type='application/json')

This assumes you actually meant to output a single JSON object as your response instead of two encodings of independent objects separated by a newline character.

πŸ‘€David Sanders

0πŸ‘

You are overwriting geoinfo every time in your loop. You are not storing all the results. Consider creating an empty list, appending to it, and sending that back as a response

πŸ‘€sedavidw

Leave a comment