Question-er XDD Question-er XDD - 3 months ago 8
JSON Question

How to get the corresponding json data in different { } and [ ] (using Python)?

I am trying to use Python Flask to process JSON data.

Here is what my data looks like:

[
{ 'className': 'BoardGame',
'fields': {
'objectId': {'type': 'String'},
'playerName': {'type': 'String'},
'cheatMode': {'type': 'Boolean'},
'score': {'type': 'Number'},
'updatedAt': {'type': 'Date'},
'createdAt': {'type': 'Date'}
},
'classLevelPermissions': {
'addField': {'*': True},
'create': {'*': True}, 'get': {'*': True},
'update': {'*': True}, 'find': {'*': True},
'delete': {'*': True}
}
},
{ 'className': 'RPGGAme',
'fields': {
'objectId': {'type': 'String'},
'GameId': {'type': 'String'},
'robotName': {'type': 'String'},
'updatedAt': {'type': 'Date'},
'createdAt': {'type': 'Date'}
},
'classLevelPermissions': {
'addField': {'*': True},
'create': {'*': True}, 'get': {'*': True},
'fields': {'*': True}, 'find': {'*': True},
'delete': {'*': True}
}
}
]


The problem is that I cannot get the corresponding data due to the complex format of
[]
and
{}


Here is what I want:

[0]["className"]: "BoardGame" <---String
[0]["fields"]: ["objectId","playerName","cheatMode"....] <-- List Of String
[1]["className"]: "RPGGame" <---String
[1]["fields"]: ["objectId","GameId",....] <-- List Of String


Thus I tried the code here:

def findkeys(node, kv):
if isinstance(node, list):
for i in node:
for x in findkeys(i, kv):
yield x
elif isinstance(node, dict):
if kv in node:
yield node[kv]
for j in node.values():
for x in findkeys(j, kv):
yield x


However, it can only find the special key's value, but cannot set the range (what I mean by range is that the first "
{}
" as array 0 + the second "
{}
" as array 1+ the "fields" in classLevelPermissions won't be involved in the result, in some sense I guess is the depth level ). I still cannot resolve the problem and get what I want.

How can I set the depth and put to corresponding array e.g. [0],[1]?

Answer
data = [
    {   'className': 'BoardGame', 
        'fields': {
            'objectId': {'type': 'String'}, 
            'playerName': {'type': 'String'}, 
            'cheatMode': {'type': 'Boolean'}, 
            'score': {'type': 'Number'}, 
            'updatedAt': {'type': 'Date'}, 
            'createdAt': {'type': 'Date'}
        }, 
        'classLevelPermissions': {
            'addField': {'*': True}, 
            'create': {'*': True}, 'get': {'*': True}, 
            'update': {'*': True}, 'find': {'*': True}, 
            'delete': {'*': True}
        }
    }, 
    {   'className': 'RPGGAme', 
        'fields': {
            'objectId': {'type': 'String'}, 
            'GameId': {'type': 'String'}, 
            'robotName': {'type': 'String'}, 
            'updatedAt': {'type': 'Date'}, 
            'createdAt': {'type': 'Date'}
        }, 
        'classLevelPermissions': {
            'addField': {'*': True}, 
            'create': {'*': True}, 'get': {'*': True}, 
            'fields': {'*': True}, 'find': {'*': True}, 
            'delete': {'*': True}
        }
    }
]

Try this:

oput = {}
for idx, dict in enumerate(data):
    oput[idx] = {}
    oput[idx]["className"] = dict["className"]
    oput[idx]["fields"] = dict["fields"].keys()

then:

oput[0]["className"]
>> 'BoardGame'

oput[0]["fields"]
>>  ['objectId', 'playerName', 'cheatMode', 'score', 'updatedAt', 'createdAt']

oput[1]["className"]
>> 'RPGGAme'

oput[1]["fields"]
>> ['robotName', 'GameId', 'createdAt', 'objectId', 'updatedAt']