I have a JSON file and I would like to get 'id' value and 'key' value for each champion:
Here example with 2 champions of my champion.json file, but if I have 100 champions how can I do that?
{
"type": "champion",
"format": "standAloneComplex",
"version": "9.23.1",
"data": {
"Aatrox": {
"version": "9.23.1",
"id": "Aatrox",
"key": "266",
"name": "Aatrox",
"title": "the Darkin Blade"
},
"Ahri": {
"version": "9.23.1",
"id": "Ahri",
"key": "103",
"name": "Ahri",
"title": "the Nine-Tailed Fox"
}
}
}
My python file :
import json
all_data = open('champion.json', encoding="utf8")
data_champ = json.load(all_data)
for element in data_champ['data']:
print(data_champ[element]["key"])
print(data_champ[element]['id'])
In the structure of the JSON data in your question, the value associated with the data key is a dictionary-of-dictionaries, so you would need to access the value of each one of them like this:
import json
with open('champion.json', encoding="utf8") as all_data:
data_champ = json.load(all_data)
for value in data_champ['data'].values():
print(value["key"])
print(value['id'])
Output:
266
Aatrox
103
Ahri
I also changed the file handling to ensure it gets closed properly by using a with statement.
Related
{
"meta": {
"code": 200
},
"response": {
"holidays": [
{
"name": "New Year's Day",
"description": "New Year\u2019s Day is celebrated many countries such as in India on the January 1 in the Gregorian calendar.",
"country": {
"id": "in",
"name": "India"
},
"date": {
"iso": "2021-01-01",
"datetime": {
"year": 2021,
"month": 1,
"day": 1
}
},
"type": [
"Optional holiday"
],
"locations": "All",
"states": "All"
},
{
"name": "Lohri",
"description": "Lohri is a restricted holiday in India",
"country": {
"id": "in",
"name": "India"
},
"date": {
"iso": "2021-01-13",
"datetime": {
"year": 2021,
"month": 1,
"day": 13
}
},
"type": [
"National holiday"
],
"locations": "All",
"states": "All"
}
]
}
}
This is my json file saved as dates.json.
I want to search for New Year's day value in the key name and then get the value of the iso key.
How can i do that?
I am a beginner in python and json. so please tell me how to do this
Is there any search algorith or a library that can help me?
One way to do it is using the modules json and jsonpath-rw-ext.
Use json module to read the json file and jsonpath-rw-ext to parse/filter.
https://github.com/sileht/python-jsonpath-rw-ext
I made it work for your example. Take a look at this.
#!/usr/bin/env python3
import json
import jsonpath_rw_ext
with open('dates.json') as json_file:
data = json.load(json_file)
result = jsonpath_rw_ext.parse("$..holidays[?(#.name=='New Year\\'s Day')]").find(data)
print([x.value for x in result])
To get the iso value use the code below.
#!/usr/bin/env python3
import json
import jsonpath_rw_ext
with open('dates.json') as json_file:
data = json.load(json_file)
result = jsonpath_rw_ext.parse("$..holidays[?(#.name=='New Year\\'s Day')].date.iso").find(data)
print(result[0].value)
I have a JSON file that looks structurally like this:
{
"content": [
{
"name": "New York",
"id": "1234",
"Tags": {
"hierarchy": "CITY"
}
},
{
"name": "Los Angeles",
"id": "1234",
"Tags": {
"hierarchy": "CITY"
}
},
{
"name": "California",
"id": "1234",
"Tags": {
"hierarchy": "STATE"
}
}
]
}
And as an outcome I would like a table view in CSV like so:
tag.key
tag.value
occurrance
hierarchy
CITY
2
hierarchy
STATE
1
Meaning I want to count the occurance of each unique "tag" in my json file and create an output csv that shows this. My original json is a pretty large file.
Firstly construct a dictionary object by using ast.literal_eval function, and then split this object to get a key, value tuples in order to create a dataframe by using zip. Apply groupby to newly formed dataframe, and finally create a .csv file through use of df_agg.to_csv such as
import json
import ast
import pandas as pd
Js= """{
"content": [
{
"name": "New York",
"id": "1234",
"Tags": {
"hierarchy": "CITY"
}
},
....
....
{
"name": "California",
"id": "1234",
"Tags": {
"hierarchy": "STATE"
}
}
]
}"""
data = ast.literal_eval(Js)
key = []
value=[]
for i in list(range(0,len(data['content']))):
value.append(data['content'][i]['Tags']['hierarchy'])
for j in data['content'][i]['Tags']:
key.append(j)
df = pd.DataFrame(list(zip(key, value)), columns =['tag.key', 'tag.value'])
df_agg=df.groupby(['tag.key', 'tag.value']).size().reset_index(name='occurrance')
df_agg.to_csv(r'ThePath\\to\\your\\file\\result.csv',index = False)
I'm new to python. I'm running python on Azure data bricks. I have a .json file. I'm putting the important fields of the json file here
{
"school": [
{
"schoolid": "mr1",
"board": "cbse",
"principal": "akseal",
"schoolName": "dps",
"schoolCategory": "UNKNOWN",
"schoolType": "UNKNOWN",
"city": "mumbai",
"sixhour": true,
"weighting": 3,
"paymentMethods": [
"cash",
"cheque"
],
"contactDetails": [
{
"name": "picsa",
"type": "studentactivities",
"information": [
{
"type": "PHONE",
"detail": "+917597980"
}
]
}
],
"addressLocations": [
{
"locationType": "School",
"address": {
"countryCode": "IN",
"city": "Mumbai",
"zipCode": "400061",
"street": "Madh",
"buildingNumber": "80"
},
"Location": {
"latitude": 49.313885,
"longitude": 72.877426
},
I need to create a data frame with schoolName as one column & latitude & longitude are others two columns. Can you please suggest me how to do that?
you can use the method json.load(), here's an example:
import json
with open('path_to_file/file.json') as f:
data = json.load(f)
print(data)
use this
import json # built-in
with open("filename.json", 'r') as jsonFile:
Data = jsonFile.load()
Data is now a dictionary of the contents exp.
for i in Data:
# loops through keys
print(Data[i]) # prints the value
For more on JSON:
https://docs.python.org/3/library/json.html
and python dictionaries:
https://www.programiz.com/python-programming/dictionary#:~:text=Python%20dictionary%20is%20an%20unordered,when%20the%20key%20is%20known.
I need to make a get (id, name, fraction id) for each deputy in this json
{
"id": "75785",
"title": "(за основу)",
"asozdUrl": null,
"datetime": "2011-12-21T12:20:26+0400",
"votes": [
{
"deputy": {
"id": "99111772",
"name": "Абалаков Александр Николаевич",
"faction": {
"id": "72100004",
"title": "КПРФ"
}
},
"result": "accept"
},
{
"deputy": {
"id": "99100491",
"name": "Абдулатипов Рамазан Гаджимурадович",
"faction": {
"id": "72100024",
"title": "ЕР"
}
},
"result": "none"
}
.......,` etc
My code is looks like that:
urlData = "https://raw.githubusercontent.com/data-dumaGovRu/vote/master/poll/2011-12-21/75785.json"
response = urllib.request.urlopen(urlData)
content = response.read()
data = json.loads(content.decode("utf8"))
for i in data:
#print(data["name"])
`
And i dont know what to do with that #print line, how I should write it?
You can access the list containing the deputies with data['votes']. Iterating through the list, you can access the keys you're interested in as you would with dict key lookups. Nested dicts imply you have to walk through the keys starting from the root to your point of interest:
for d in data['votes']:
print(d['deputy']['id'], d['deputy']['name'], d['deputy']['faction']['id'])
I have this JSON file:
{
"annotations": [{
"title": "Autismo",
"spot": "Autismo",
"uri": "http://it.wikipedia.org/wiki/Autismo",
"categories": ["Autismo", "Disturbi psichici", "Malattie del sistema nervoso", "Pediatria"]
}, {
"title": "Tablet computer",
"spot": "tablet",
"uri": "http://it.wikipedia.org/wiki/Tablet_computer",
"categories": ["Computer tablet"]
}, {
"title": "Diagnosi",
"spot": "diagnosi precoce",
"uri": "http://it.wikipedia.org/wiki/Diagnosi",
"categories": ["Semeiotica", "Diagnostica medica"]
}]
}
My target is to parse the JSON file and to create a new one with a changed structure like this:
{u'http://it.wikipedia.org/wiki/Autismo': {'spot': u'autistica',
'title': u'Autismo'
'categories':["Autismo", "Disturbi psichici", "Malattie del sistema nervoso", "Pediatria"]},
And so on for every dictionary into annotations.
My code is this:
diz={}
values = json.load(jsonFile)
for key in values["annotations"]:
uri= key["uri"]
ddd=key["categories"]
diz[uri]={"title":key["title"],"spot":key["spot"],"categories":ddd}
print diz
But when I run the code it gives me an error on ddd=key["categories"]. I think that I don't get in a correct way the values of "categories".
Can somebody help me in this?