Home > database >  Parse Json with Python where a single object contains 200 objects, and I want to operate on each obj
Parse Json with Python where a single object contains 200 objects, and I want to operate on each obj

Time:02-10

So I have an object I imported into a dict.

{"results":[
{"Name":"fed-sv01","NodeID":118,"XID":"37be","RowID":"2","Vendor":"Wnet","Status":"0"},
{"Name":"Joe-sv02","NodeID":119,"XID":"ab39","RowID":"2","Vendor":"Wnet","Status":"92"},
...
{"Name":"May-sv01","NodeID":703,"XID":"zc93","RowID":"2","Vendor":"Wnet","Status":"4"}]}

I show 3 lines, but there are around 200 similar lines.

So "results":[] is a top level container that simply encapsulated the set of objects.

What I want to do is iterate through the nested objects:

{"Name":"fed-sv01","NodeID":118,"XID":"37be","RowID":"2","Vendor":"Wnet","Status":"0"}

And build a different json object:

{"Name":"fred-sv01","Status":"0"}

That I will then push out to another system set up to catch these objects. I have the code for that, what I'm trying to do is peel the json objects from inside results[] one at a time, and I'm not finding any examples of how to do that. I could just run a command on the json to remove the results[] wrapper, but that seems silly if all I have to do is deal with results[] inside the scripts that do the iteration.

I want to read a line, create a new object, ship the object off. Rinse & repeat until iterated through all 200 objects. We could pop them off as we iterate if that makes it easier, but I'm not sure it does. The original object is ephemeral, so it can be destroyed.

Does anyone have some example code showing how to parse json in a results[] object with python?

The comments are helping, this is what I have now:

import json

with open('json.results') as json_file:
    data = json.load(json_file)


    for row in data['results']:
            print(row)  #good so far

            node = {row('Name'), row('Status')}
            for item in node():
                print(item) #blows up with "TypeError: 'dict' object is not callable"

CodePudding user response:

Making Barmar`s comment and mine into an answer:

for item in container['results']: will iterate over the list of objects, and with newDict = {item['Name'], item['Status']} you can build a new object capturing the keys of the first and then do the rest of the work using this new object.

CodePudding user response:

Ok, I have the code I need now. Thanks to Barmar and Hiero De Paula, they both contributed to my effort.

#!/usr/bin/env python3
import json

with open('json.to.import') as json_file:
    data = json.load(json_file)


    for i in data['results']:
            print(i)  #good so far
            print(type(i))

            node = {}
            node['Name'] = i['Name']
            node['Status'] = i['Status']

            print(type(node))
            print(node)
            print("Name ",  node['Name'])
            print("Status ",  node['Status'])

From this point I add a couple fields to the dict node, and then execute the push into the other system, but the print statements confirm I have accomplished whet I needed help with.

  • Related