appending onto JSON in python - python

I have a JSON file that has peoples names in and lists there children.
people.json:
{
"person": [
{
"Id": 0,
"firstName": "Bob",
"lastName": "Bruce",
"children": [
{
"Id": 0,
"Name": "Phil",
},
{
"Id": 1,
"Name": "Dave",
}
]
},
{
"Id": 1,
"firstName": "Fred",
"lastName": "Gone",
"children": [
{
"Id": 0,
"Name": "Harry",
}
]
}
]
}
I want to be able to add a child onto a person. How would I go about doing this. This is my attempt to add a child onto Fred.
people.py
import json
json_data = open("people.json")
data = json.load(json_data)
for d in data['person']:
if d['firstName'] == "Fred":
d['children'] + [{u'Id': 1, u'Name': u'Rich'}]
print d['children']
When it prints out it only prints out the existing data and not what I have tried to add

Your line:
d['children'] + [{u'Id': 1, u'Name': u'Rich'}]
isn't actually adding it in, just calculating it. You can do
d['children'].append({u'Id': 1, u'Name': u'Rich'})

This line:
d['children'] + [{u'Id': 1, u'Name': u'Rich'}]
changes nothing.
d['children'].append({u'Id': 1, u'Name': u'Rich'})

Related

JSON children and parent elements access on python

I am making JSON structure.
I need access from id3 to id4 (one step forward)
I need access from id4 to id3 (one step back)
I have to be able to search into children elements
Not using format json_result[1]["next"]["next"][1]["next"][0]["Name"]
Link to a source with exact lesson will be fine - I couldn't find one :(
Thank you.
json_result = json
json_last = json_result # Using json_last as pointer to a record into json_result
. . .
# part of a cycle reading doc
json_result_temp = {
"Name":item_name_clean,
"id":item_autoname,
"lvl":json_structure_lvl,
#"prev": {},
"prev": int,
"next": {}
}
if json_structure_lvl == 0:
json_result = json_result_temp # writing first element
json_last = json_result # first element don't have previuos elements
else:
#json_result_temp["prev"] = json_last # get a error when trying to convert into JSON
json_result_temp["prev"] = int(id(json_last)) # trying to store adress of previous element
json_last["next"] = json_result_temp # adding current record "json_result_temp" into "next" field of previus record
json_last = json_last["next"] # changing point to the child element
[{
"Name": "Name 1",
"id": "id0",
"lvl": 0,
"prev": {},
"next": {
"Name": "Name 1.1",
"id": "id1",
"lvl": 1,
"prev": 2536898825856,
"next": [
{"Name": "Name 1.1.1",
"id": "id2",
"lvl": 2,
"prev": 2536898748864,
"next": {}},
{"Name": "Name 1.1.2",
"id": "id3",
"lvl": 2,
"prev": 2536898823424,
"next": [
{"Name": "Name 1.1.2.1",
"id": "id4",
"lvl": 3,
"prev": 2536898827072,
"next": {}},
{"Name": "Name 1.1.2.2",
"id": "id5",
"lvl": 3,
"prev": 2536898828782,
"next": {}}
]}
]
},
{ "Name": "Name 2",
"id": "id6",
"lvl": 0,
"prev": {},
"next": {
"Name": "Name 2.1",
"id": "id7",
"lvl": 1,
"prev": 2536898825856,
"next": [
{"Name": "Name 2.1.1",
"id": "id8",
"lvl": 2,
"prev": 2536898748864,
"next": {}},
{"Name": "Name 2.1.2",
"id": "id9",
"lvl": 2,
"prev": 2536898823424,
"next": [
{"Name": "Name 2.1.2.1",
"id": "id10",
"lvl": 3,
"prev": 2536898827072,
"next": {}},
{"Name": "Name 2.1.2.2",
"id": "id11",
"lvl": 3,
"prev": 2536898827888,
"next": {}}
]}
]
}
}]
I made "prev" and try to store there addresses location for the previuos element, but when I try to convert the results into the JSON format I get an error. How can I get access to the previous elements not using format json_result[1]["next"]["next"][1]["next"][0]["Name"], but using only pointer or something else. Because depth of children elements can be changed, and I don't know the exact path, I have to search one by one.

how do I access this json data in python?

hi I'm pretty new at coding and I was trying to create a program in python that reads and save in another file the data inside a json file (not everything, just what I want). I googled how to parse data but there's something I don't understand.
that's a part of the json file:
`
{
"profileRevision": 548789,
"profileId": "campaign",
"profileChangesBaseRevision": 548789,
"profileChanges": [
{
"changeType": "fullProfileUpdate",
"profile": {
"_id": "2da4f079f8984cc48e84fc99dace495d",
"created": "2018-03-29T11:02:15.190Z",
"updated": "2022-10-31T17:34:43.284Z",
"rvn": 548789,
"wipeNumber": 9,
"accountId": "63881e614ef543b2932c70fed1196f34",
"profileId": "campaign",
"version": "refund_teddy_perks_september_2022",
"items": {
"8ec8f13f-6bf6-4933-a7db-43767a055e66": {
"templateId": "Quest:heroquest_loadout_constructor_2",
"attributes": {
"quest_state": "Claimed",
"creation_time": "min",
"last_state_change_time": "2019-05-18T16:09:12.750Z",
"completion_complete_pve03_diff26_loadout_constructor": 300,
"level": -1,
"item_seen": true,
"sent_new_notification": true,
"quest_rarity": "uncommon",
"xp_reward_scalar": 1
},
"quantity": 1
},
"6940c71b-c74b-4581-9f1e-c0a87e246884": {
"templateId": "Worker:workerbasic_sr_t01",
"attributes": {
"gender": "2",
"personality": "Homebase.Worker.Personality.IsDreamer",
"level": 1,
"item_seen": true,
"squad_slot_idx": -1,
"portrait": "WorkerPortrait:IconDef-WorkerPortrait-Dreamer-F02",
"building_slot_used": -1,
"set_bonus": "Homebase.Worker.SetBonus.IsMeleeDamageLow"
}
}
}
]
}
`
I can access profileChanges. I wrote this to create another json file with only the profileChanges things:
`
myjsonfile= open("file.json",'r')
jsondata=myjsonfile.read()
obj=json.loads(jsondata)
ciso=obj['profileChanges']
for i in ciso:
print(i)
with open("file2", "w") as outfile:
json.dump( ciso, outfile, indent=1)
the issue I have is that I can't access "profile" (inside profileChanges) in the same way by parsing the new file and I have no idea on how to do it
Access to JSON or dict element is realized by list indexes, please look at below example:
a = [
{
"friends": [
{
"id": 0,
"name": "Reba May"
}
],
"greeting": "Hello, Doris Gallagher! You have 2 unread messages.",
"favoriteFruit": "strawberry"
},
]
b = a['friends']['id] # b = 0
I've added a couple of closing braces to make your snippet valid json:
s = '''{
"profileRevision": 548789,
"profileId": "campaign",
"profileChangesBaseRevision": 548789,
"profileChanges": [
{
"changeType": "fullProfileUpdate",
"profile": {
"_id": "2da4f079f8984cc48e84fc99dace495d",
"created": "2018-03-29T11:02:15.190Z",
"updated": "2022-10-31T17:34:43.284Z",
"rvn": 548789,
"wipeNumber": 9,
"accountId": "63881e614ef543b2932c70fed1196f34",
"profileId": "campaign",
"version": "refund_teddy_perks_september_2022",
"items": {
"8ec8f13f-6bf6-4933-a7db-43767a055e66": {
"templateId": "Quest:heroquest_loadout_constructor_2",
"attributes": {
"quest_state": "Claimed",
"creation_time": "min",
"last_state_change_time": "2019-05-18T16:09:12.750Z",
"completion_complete_pve03_diff26_loadout_constructor": 300,
"level": -1,
"item_seen": true,
"sent_new_notification": true,
"quest_rarity": "uncommon",
"xp_reward_scalar": 1
},
"quantity": 1
},
"6940c71b-c74b-4581-9f1e-c0a87e246884": {
"templateId": "Worker:workerbasic_sr_t01",
"attributes": {
"gender": "2",
"personality": "Homebase.Worker.Personality.IsDreamer",
"level": 1,
"item_seen": true,
"squad_slot_idx": -1,
"portrait": "WorkerPortrait:IconDef-WorkerPortrait-Dreamer-F02",
"building_slot_used": -1,
"set_bonus": "Homebase.Worker.SetBonus.IsMeleeDamageLow"
}
}
}
}
}
]
}
'''
d = json.loads(s)
print(d['profileChanges'][0]['profile']['version'])
This prints refund_teddy_perks_september_2022
Explanation:
d is a dict
d['profileChanges'] is a list of dicts
d['profileChanges'][0] is the first dict in the list
d['profileChanges'][0]['profile'] is a dict
d['profileChanges'][0]['profile']['version'] is the value of version key in the profile dict in the first entry of the profileChanges list.

How to extract JSON from a nested JSON file?

I am calling an API and getting a response like the below.
{
"status": 200,
"errmsg": "OK",
"data": {
"total": 12,
"items": [{
"id": 11,
"name": "BBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChain": {
"inAlerting": false,
"throttlingAlerts": 20,
"enableThrottling": true,
"name": "Example123",
"destination": [],
"description": "",
"ccdestination": [],
"id": 3,
"throttlingPeriod": 10
}
},
{
"id": 21,
"name": "CNBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChain": {
"inAlerting": false,
"throttlingAlerts": 20,
"enableThrottling": true,
"name": "Example456",
"destination": [],
"description": "",
"ccdestination": [],
"id": 3,
"throttlingPeriod": 10
}
}
]
}
}
I need to clean-up this JSON a bit and produce a simple JSON like below where escalatingChainName is the name in the escalatingChain list so that I can write this into a CSV file.
{
"items": [{
"id": 11,
"name": "BBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChainName": "Example123"
},
{
"id": 21,
"name": "CNBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChainName": "Example456"
}
]
}
Is there a JSON function that I can use to copy only the necessary key-value or nested key-values to a new JSON object?
With the below code, I am able to get the details list.
json_response = response.json()
items = json_response['data']
details = items['items']
I can print individual list items using
for x in details:
print(x)
How do I take it from here to pull only the necessary fields like id, name, priority and the name from escalatingchain to create a new list or JSON?
There is no existing function that will do what you want, so you'll need to write one. Fortunately that's not too hard in this case — basically you just create a list of new items by extracting the pieces of data you want from the existing ones.
import json
json_response = """\
{
"status": 200,
"errmsg": "OK",
"data": {
"total": 12,
"items": [{
"id": 11,
"name": "BBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChain": {
"inAlerting": false,
"throttlingAlerts": 20,
"enableThrottling": true,
"name": "Example123",
"destination": [],
"description": "",
"ccdestination": [],
"id": 3,
"throttlingPeriod": 10
}
},
{
"id": 21,
"name": "CNBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChain": {
"inAlerting": false,
"throttlingAlerts": 20,
"enableThrottling": true,
"name": "Example456",
"destination": [],
"description": "",
"ccdestination": [],
"id": 3,
"throttlingPeriod": 10
}
}
]
}
}
"""
response = json.loads(json_response)
cleaned = []
for item in response['data']['items']:
cleaned.append({'id': item['id'],
'name': item['name'],
'priority': item['priority'],
'levelStr': item['levelStr'],
'escalatingChainId': item['escalatingChainId'],
'escalatingChainName': item['escalatingChain']['name']})
print('cleaned:')
print(json.dumps(cleaned, indent=4))
You can try:
data = {
"status": 200,
"errmsg": "OK",
"data": {
"total": 12,
"items": [{
"id": 11,
"name": "BBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChain": {
"inAlerting": False,
"throttlingAlerts": 20,
"enableThrottling": True,
"name": "Example123",
"destination": [],
"description": "",
"ccdestination": [],
"id": 3,
"throttlingPeriod": 10
}
},
{
"id": 21,
"name": "CNBC",
"priority": 4,
"levelStr": "All",
"escalatingChainId": 3,
"escalatingChain": {
"inAlerting": False,
"throttlingAlerts": 20,
"enableThrottling": True,
"name": "Example456",
"destination": [],
"description": "",
"ccdestination": [],
"id": 3,
"throttlingPeriod": 10
}
}
]
}
}
for single_item in data["data"]["items"]:
print(single_item["id"])
print(single_item["name"])
print(single_item["priority"])
print(single_item["levelStr"])
print(single_item["escalatingChain"]["inAlerting"])
# and so on
Two ways of approaching this depending on whether your dealing with a variable or .json file using python list and dictionary comprehension:
Where data variable of type dictionary (nested) already defined:
# keys you want
to_keep = ['id', 'name', 'priority', 'levelStr', 'escalatingChainId',
'escalatingChainName']
new_data = [{k:v for k,v in low_dict.items() if k in to_keep}
for low_dict in data['data']['items']]
# where item is dictionary at lowest level
escalations = [{v+'Name':k[v]['name']} for k in data['data']['items']
for v in k if type(k[v])==dict]
# merge both lists of python dictionaries to produce flattened list of dictionaries
new_data = [{**new,**escl} for new,escl in zip(new_data,escalations)]
Or (and since your refer json package) if you have save the response to as a .json file:
import json
with open('response.json', 'r') as handl:
data = json.load(handl)
to_keep = ['id', 'name', 'priority', 'levelStr', 'escalatingChainId',
'escalatingChainName']
new_data = [{k:v for k,v in low_dict.items() if k in to_keep}
for low_dict in data['data']['items']]
escalations = [{v+'Name':k[v]['name']} for k in data['data']['items']
for v in k if type(k[v])==dict]
new_data = [{**new,**escl} for new,escl in zip(new_data,escalations)]
Both produce output:
[{'id': 11,
'name': 'BBC',
'priority': 4,
'levelStr': 'All',
'escalatingChainId': 3,
'escalatingChainName': 'Example123'},
{'id': 21,
'name': 'CNBC',
'priority': 4,
'levelStr': 'All',
'escalatingChainId': 3,
'escalatingChainName': 'Example456'}]

Extract data from deeply-nested JSON

I am new to JSON data and I am trying to get down to a very deeply nested dictionary that sits inside of 3 arrays and a dictionary of a JSON file. I am trying to get to the dictionary with the label "player". I am trying to extract the players for all teams to then move into a DataFrame so I can use for other analysis.
How do i write loop to get to this data?
This is what I'm trying to do but it doesn't work:
rostered = json.dumps(rostered_players)
player_value = [[
values['playerPoolEntry']['player']
]for values in rostered]
Here is a sample of data that I am working with:
rostered =
'''[
[
{
"appliedStatTotal": 260.05,
"entries": [
[
{
"injuryStatus": "NORMAL",
"lineupSlotId": 2,
"playerId": 2977644,
"playerPoolEntry": {
"appliedStatTotal": 37.6,
"id": 2977644,
"keeperValue": 56,
"keeperValueFuture": 56,
"lineupLocked": false,
"onTeamId": 1,
"player": {
"firstName": "Todd",
"fullName": "Todd Gurley II",
"id": 2977644,
"injuryStatus": "ACTIVE",
"lastName": "Gurley II"
},
"rosterLocked": false,
"tradeLocked": false
}
}
]
]
}
],
[
{
"appliedStatTotal": 260.05,
"entries": [
[
{
"injuryStatus": "NORMAL",
"lineupSlotId": 4,
"playerId": 3892889,
"playerPoolEntry": {
"appliedStatTotal": 6.6000000000000005,
"id": 3892889,
"keeperValue": 5,
"keeperValueFuture": 5,
"lineupLocked": false,
"onTeamId": 2,
"player": {
"defaultPositionId": 3,
"firstName": "Dede",
"fullName": "Dede Westbrook",
"id": 3892889,
"lastName": "Westbrook"
},
"rosterLocked": false,
"tradeLocked": false
}
}
]
]
}
]
]'''
One way to do this is to use recursion. This is a more functional approach.
from pprint import pprint
def iterate_deep(item):
if isinstance(item, dict):
for key, val in item.items():
if key == 'player':
pprint(val)
else:
iterate_deep(val)
elif isinstance(item, list):
for deeper_item in item:
iterate_deep(deeper_item)
iterate_deep(rostered_players)
A possible solution could be parsing the given JSON as it is.
rostered=json.loads(rostered)
player_values=[]
for level0 in rostered:
for level1 in level0:
if 'entries' in level1:
level2 = level1.get('entries')
for level3 in level2:
for level4 in level3:
if 'playerPoolEntry' in level4 and 'player' in level4.get('playerPoolEntry'):
player_values.append(level4.get('playerPoolEntry').get('player'))

How to update only one key value of nested json in robot framework

I am new to Robot Framework. I have a Nested Json like below -
"db_result": {
"db_result_emp": [
{
"emp_id": 1110,
"name": "abc",
"address": 2,
"street1": 1,
"street2": 0,
},
{
"emp_id": 1111,
"name": "xyz",
"address": 2,
"street1": 1,
"street2": 0,
}
]
i want to update name of emp id 1110 to ```"name":"zzz" i tried below keywords
${updated_json_value}= Update Value To Json ${json_sub_header_key_content} $..name ${emp_name}
But above keyword updates for both the emp_id's i.e 1110, 1111
How can i update the key value for only emp_id 1110?
First of all a little correction to your posted JSON. You have trailing commas in your objects, which isn't allowed, so you need to remove those:
{
"db_result": {
"db_result_emp": [
{
"emp_id": 1110,
"name": "abc",
"address": 2,
"street1": 1,
"street2": 0
},
{
"emp_id": 1111,
"name": "xyz",
"address": 2,
"street1": 1,
"street2": 0
}
]
}
}
You could use this JSONPath
$..[?(#.emp_id==1110)].name

Categories

Resources