Nested JSON not loading as Keys in dict - python

I have the below JSON currently in a dict called returned_data- I would like to get the account ID out, but it looks like it is not recognizing any keys deeper than the 2nd level. eg:
returned_data["command1"]["customers"]
works
returned_data["command1"]["customers"]["acctid"]
does not work.
returned_data["command1"]["customers"].keys()
does not display any keys.
What do I need to do to be able to reference the acctid?
{
"command1": {
"customers": [{
"city": "none",
"cust_id": 204567,
"name_first": "John",
"name_last": "Smith",
"zip": "39199",
"street_addr_1": "1 Bat St",
"phones": [
{"phone_number": "(01) 5555555",
"phone_type": "Mobile",
"phone_code": "C"
},
{"phone_number": "(01) 5555555",
"phone_type": "Home",
"phone_code": "E"
}
],
"email_addr": "test#test.com",
"acctid": 123456,
"state": "WA",
"add_user": "JR",
"country": "AUS",
"acct_type": "P",
}
],
"ref": "123456",
"result": 0
},
"header": {"src_sys_type": 2,
"ver": 1,
"result": 0}
}

This is what you'll need.
['command1']['customers'][0]['acctid']
You're probably getting an index error. The [0] is needed because of the bracket after 'customers'. As the comment said, it's a list, not a json.

Related

Getting all the Keys from JSON Object?

Goal: To create a script that will take in nested JSON object as input and output a CSV file with all keys as rows in the CSV?
Example:
{
"Document": {
"DocumentType": 945,
"Version": "V007",
"ClientCode": "WI",
"Shipment": [
{
"ShipmentHeader": {
"ShipmentID": 123456789,
"OrderChannel": "Shopify",
"CustomerNumber": 234234,
"VendorID": "2343SDF",
"ShipViaCode": "FEDX2D",
"AsnDate": "2018-01-27",
"AsnTime": "09:30:47-08:00",
"ShipmentDate": "2018-01-23",
"ShipmentTime": "09:30:47-08:00",
"MBOL": 12345678901234568,
"BOL": 12345678901234566,
"ShippingNumber": "1ZTESTTEST",
"LoadID": 321456987,
"ShipmentWeight": 10,
"ShipmentCost": 2.3,
"CartonsTotal": 2,
"CartonPackagingCode": "CTN25",
"OrdersTotal": 2
},
"References": [
{
"Reference": {
"ReferenceQualifier": "TST",
"ReferenceText": "Testing text"
}
}
],
"Addresses": {
"Address": [
{
"AddressLocationQualifier": "ST",
"LocationNumber": 23234234,
"Name": "John Smith",
"Address1": "123 Main St",
"Address2": "Suite 12",
"City": "Hometown",
"State": "WA",
"Zip": 92345,
"Country": "USA"
},
{
"AddressLocationQualifier": "BT",
"LocationNumber": 2342342,
"Name": "Jane Smith",
"Address1": "345 Second Ave",
"Address2": "Building 32",
"City": "Sometown",
"State": "CA",
"Zip": "23665-0987",
"Country": "USA"
}
]
},
"Orders": {
"Order": [
{
"OrderHeader": {
"PurchaseOrderNumber": 23456342,
"RetailerPurchaseOrderNumber": 234234234,
"RetailerOrderNumber": 23423423,
"CustomerOrderNumber": 234234234,
"Department": 3333,
"Division": 23423,
"OrderWeight": 10.23,
"CartonsTotal": 2,
"QTYOrdered": 12,
"QTYShipped": 23
},
"Cartons": {
"Carton": [
{
"SSCC18": 12345678901234567000,
"TrackingNumber": "1ZTESTTESTTEST",
"CartonContentsQty": 10,
"CartonWeight": 10.23,
"LineItems": {
"LineItem": [
{
"LineNumber": 1,
"ItemNumber": 1234567890,
"UPC": 9876543212,
"QTYOrdered": 34,
"QTYShipped": 32,
"QTYUOM": "EA",
"Description": "Shoes",
"Style": "Tall",
"Size": 9.5,
"Color": "Bllack",
"RetailerItemNumber": 2342333,
"OuterPack": 10
},
{
"LineNumber": 2,
"ItemNumber": 987654321,
"UPC": 7654324567,
"QTYOrdered": 12,
"QTYShipped": 23,
"QTYUOM": "EA",
"Description": "Sunglasses",
"Style": "Short",
"Size": 10,
"Color": "White",
"RetailerItemNumber": 565465456,
"OuterPack": 12
}
]
}
}
]
}
}
]
}
}
]
}
}
In the above JSON Object, I want all the keys (nested included) in a List (Duplicates can be removed by using a set Data Structure). If Nested Key Occurs like in actual JSON they can be keys multiple times in the CSV !
I personally feel that recursion is a perfect application for this type of problem if the amount of nests you will encounter is unpredictable. Here I have written an example in Python of how you can utilise recursion to extract all keys. Cheers.
import json
row = ""
def extract_keys(data):
global row
if isinstance(data, dict):
for key, value in data.items():
row += key + "\n"
extract_keys(value)
elif isinstance(data, list):
for element in data:
extract_keys(element)
# MAIN
with open("input.json", "r") as rfile:
dicts = json.load(rfile)
extract_keys(dicts)
with open("output.csv", "w") as wfile:
wfile.write(row)

navigating json table in python

I am trying to access the team name key value and the american key value
print(bv_json['outcomes'][0]['description'])
the parts of the json table that I need are denoted with the ########### trailing near the end of the table posted, I get an error about needing an integer to iterate rather than string, I am also struggling with navigating through the keys
thanks
[
{
"path": [
{
"id": "2958468",
"link": "/basketball/nba",
"description": "NBA",
"type": "LEAGUE",
"sportCode": "BASK",
"order": 1,
"leaf": true,
"current": true
},
{
"id": "227",
"link": "/basketball",
"description": "Basketball",
"type": "SPORT",
"sportCode": "BASK",
"order": 1,
"leaf": false,
"current": false
}
],
"events": [
{
"id": "8801181",
"description": "L.A. Clippers # Utah Jazz",
"type": "GAMEEVENT",
"link": "/basketball/nba/l-a-clippers-utah-jazz-202106082215",
"status": "O",
"sport": "BASK",
"startTime": 1623204900000,
"live": true,
"awayTeamFirst": true,
"denySameGame": "NO",
"teaserAllowed": true,
"competitionId": "2958468",
"notes": "Best of 7 - Game 1",
"numMarkets": 34,
"lastModified": 1623212024024,
"competitors": [
{
"id": "8801181-285",
"name": "Utah Jazz",
"home": true
},
{
"id": "8801181-310",
"name": "L.A. Clippers",
"home": false
}
],
"displayGroups": [
{
"id": "100-97",
"description": "Game Lines",
"defaultType": true,
"alternateType": false,
"markets": [
{
"id": "157658380",
"descriptionKey": "Head To Head",
"description": "Moneyline",
"key": "2W-12",
"marketTypeId": "3059",
"status": "O",
"singleOnly": false,
"notes": "",
"period": {
"id": "341",
"description": "Live Game",
"abbreviation": "G",
"live": true,
"main": true
},
"outcomes": [
{
"id": "849253180",
"description": "L.A. Clippers",##############
"status": "O",
"type": "A",
"competitorId": "8801181-310",
"price": {
"id": "7927852247",
"american": "+125",#########################
"decimal": "2.250",
"fractional": "5/4",
"malay": "-0.80",
"indonesian": "1.25",
"hongkong": "1.25"
It looks like your data structure is
[{[{[{[{{}}]}]}]}]
Which is a list containing a dictionary of a list of dictionaries of lists of dictionaries, which is to say it's nested and confusing.
To make it easy on yourself, I think defining some variables will help.
Let's access the first level list item, the dictionary that contains 'path'- this dict contains all the other lists of dictionaries.
full_dict = bvjson[0] # step into a list
Looking at the data, we know that outcomes is in the 'events' list of dicts, so let's define that variable to make it easier to step into for our when we get to our ultimate answer.
events = full_dict['events'] # access dictionary value by key
Now we have access to events, which is a list of dictionaries of lists of dictionaries.
In events, we see that 'outcomes' actually lives two steps into the 'displayGroups' value, so let's get 'displayGroups' into something useable.
display = events['displayGroups'][0]
# 'displayGroups' is a key in the dictionary in the event list,
# and it holds a list of dictionaries, so we use [0] to step
# into the list to access the dicts.
# Note - if there are multiple lists this will only access the first one.
Stepping in further:
markets = display['markets'][0]
outcomes = markets['outcomes'][0]
You finally have easy access to the outcomes list of dict!
description = outcomes['description']
price = outcomes['price']['american']
So remember, anytime you get a confusing nested json like this, stepping in to each value can help you figure out how to get what you want and if you need to access via index (if it's a list) or via key (if it's a dictionary).
Think of all of this as just a way to diagnose and figure out why you aren't getting the values you are requesting - it will be different for each case, and different logic will be required for handling getting multiple values out of each list or dict - but this is a good start and way to get your mind around it.
Here is your data properly enclosed:
bvjson =
[
{
"path": [
{
"id": "2958468",
"link": "/basketball/nba",
"description": "NBA",
"type": "LEAGUE",
"sportCode": "BASK",
"order": 1,
"leaf": True,
"current": True
},
{
"id": "227",
"link": "/basketball",
"description": "Basketball",
"type": "SPORT",
"sportCode": "BASK",
"order": 1,
"leaf": False,
"current": False
}
],
"events": [
{
"id": "8801181",
"description": "L.A. Clippers # Utah Jazz",
"type": "GAMEEVENT",
"link": "/basketball/nba/l-a-clippers-utah-jazz-202106082215",
"status": "O",
"sport": "BASK",
"startTime": 1623204900000,
"live": True,
"awayTeamFirst": True,
"denySameGame": "NO",
"teaserAllowed": True,
"competitionId": "2958468",
"notes": "Best of 7 - Game 1",
"numMarkets": 34,
"lastModified": 1623212024024,
"competitors": [
{
"id": "8801181-285",
"name": "Utah Jazz",
"home": True
},
{
"id": "8801181-310",
"name": "L.A. Clippers",
"home": False
}
],
"displayGroups": [
{
"id": "100-97",
"description": "Game Lines",
"defaultType": True,
"alternateType": False,
"markets": [
{
"id": "157658380",
"descriptionKey": "Head To Head",
"description": "Moneyline",
"key": "2W-12",
"marketTypeId": "3059",
"status": "O",
"singleOnly": False,
"notes": "",
"period": {
"id": "341",
"description": "Live Game",
"abbreviation": "G",
"live": True,
"main": True
},
"outcomes": [
{
"id": "849253180",
"description": "L.A. Clippers",##############
"status": "O",
"type": "A",
"competitorId": "8801181-310",
"price": {
"id": "7927852247",
"american": "+125",#########################
"decimal": "2.250",
"fractional": "5/4",
"malay": "-0.80",
"indonesian": "1.25",
"hongkong": "1.25"}
}
]
}
]
}
]
}
]
}
]

How to read JSON file with number of key in nested dictionary keeps changing

I have this JSON nested dictionary I need to parse into SQL table. The problem is the number of key tank (max is 4) in nested dictionary keeps changing with different site_id. Are there a way to read them?
{
"data": [
{
"site_id": 30183,
"city": "Seattle",
"state": "US-WA",
"tank": [
{
"id": 00001,
"name": "Diesel"
},
{
"id": 00002,
"name": "Diesel"
},
{
"id": 00003,
"name": "Unleaded 89"
}
]
},
{
"site_id": 200942,
"city": "Boise",
"state": "ID-WA",
"tank": [
{
"id": 00001,
"name": "Diesel"
},
{
"id": 00002,
"name": "Unleaded 95"
}
]
}
]
}
Here is my current code:
for site in response['data']:
row = []
row.extend([site['site_id'], site['city'], site['state']])
for tank in site['tank']:
row.extend([tank['id'], tank['name']])
Any site_id that does not have enough 4 tank can have missing value replaced with NULL
I don't know how to modify it to adjust to different number of tank keys. Any suggestion help! Thank you

How to read nested Json lists using Python?

How can I get all the accountNumber, name and phoneNumber to be printed separately in Json Response using Python
[{
"msg": "result",
"id": "testdata",
"result": [{
"accountNumber": "123456",
"name": "CHRISfarmece",
"phoneNumber": "2333455"
}, {
"accountNumber": "553222",
"name": "name1",
"phoneNumber": "123456"
}, {
"accountNumber": "34566",
"name": "name2",
"phoneNumber": "24567"
}]
}]
You can simply loop through the result value (it's a list) and store each value in separeted list like below :
data2 = {'accountNumber':[], 'name':[], 'phoneNumber':[]}
for x in data1[0]['result']:
for key, value in x.items():
data2[key].append(value)

How to get one key and value from a JSON in python?

I am getting a data in response when I hit the API URL in django. I want to get content_id from that data to check whether it already exists or not. How to iterate the JSON to find the certain key and and its value?
r.json() =
{
"code": 200,
"status": "OK",
"data": [
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "17408"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
}
],
"msg": "Contents Found!"
}
i tried r.json()['data'][0]['content'][0] but it works in only one data in, if there are more than one data then when i try to iterate content_id with for loop it doesnt work
General access
with open('vash.json') as f:
data = json.load(f)
data['data'][0]['content'][0]['content_id'])
To iterate over
for i in data['data']:
for j in i['content']:
print(j['content_id'])
Bonus
print([j['content_id'] for i in data['data'] for j in i['content']])
17408
Maybe something like this could help. In the code you posted, the first line is a little bit different from what I have seen previously. Maybe json.loads will help you in getting the "content_id" for n number of objects in content.
import json
data = json.loads('''{
"code": 200,
"status": "OK",
"data": [
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "17408"
},
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "999834"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
},
{
"cart_id": "36",
"content": [
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "34523"
},
{
"price": "100",
"price_id": "1",
"code": "USD",
"symbol": "$",
"name": "Carol of the Bells SATB - arr. Jay Rouse",
"content_id": "6423412"
}
],
"poster_url": "http://devstudio.cnedocent.com/img/No-Image-Vertical.png"
}
],
"msg": "Contents Found!"
}''')
for current_data in data["data"]:
for current_content in current_data["content"]:
# here I am printing, I imagine you will do something different
# with the content_id.
print(current_content["content_id"])
With python it's pretty easy to convert json to a dictionary and iterate it that way.
https://docs.python.org/2/library/json.html

Categories