That seems generally fine.
There's no need to first read the file, then use loads. You can just use load directly.
output_json = json.load(open('/tmp/output.json'))
Using i and k isn't correct for this. They should generally be used only for an integer loop counter. In this case they're keys, so something more appropriate would be better. Perhaps rename i as container and k as stream? Something that communicate more information will be easier to read and maintain.
You can use output_json.iteritems() to iterate over both the key and the value at the same time.
for majorkey, subdict in output_json.iteritems():
print majorkey
for subkey, value in subdict.iteritems():
print subkey, value
Note that, when using Python 3, you will need to use items() instead of iteritems(), as it has been renamed.
Answer from chmullig on Stack OverflowThat seems generally fine.
There's no need to first read the file, then use loads. You can just use load directly.
output_json = json.load(open('/tmp/output.json'))
Using i and k isn't correct for this. They should generally be used only for an integer loop counter. In this case they're keys, so something more appropriate would be better. Perhaps rename i as container and k as stream? Something that communicate more information will be easier to read and maintain.
You can use output_json.iteritems() to iterate over both the key and the value at the same time.
for majorkey, subdict in output_json.iteritems():
print majorkey
for subkey, value in subdict.iteritems():
print subkey, value
Note that, when using Python 3, you will need to use items() instead of iteritems(), as it has been renamed.
json_data = json.loads(url)
If list is there, then iterate:
for majorkey, subdict in json_data.iteritems():
for one_majorkey in subdict:
for subkey, value in one_majorkey.iteritems():
for each_subkey, valu_U in value.iteritems():
for each_sub_subkey, value_Q in valu_U.iteritems():
for each_sub_sub_subkey, value_num in value_Q.iteritems():
print each_sub_sub_subkey, value_num
I'm trying to restructure JSON output from a API to the specific format that Ansible wants for it's invetory file. To do this I thought I would loop through the JSON and grab the variables I need and then feed that into the proper structure. However, I noticed my API JSON is using the same $ key for multiple variables. How can I properly reference and differentiate between the variables? Here is an example:
"model-responses": {
"model": [
{
"@mh": "0x11e013",
"attribute": [
{
"@id": "0x1006e",
"$": "switch1"
},
{
"@id": "0x23000e",
"$": "Raritan Computer, Inc.DV"
},
{
"@id": "0x12d7f",
"$": "10.10.10.1"
},
{
"@id": "0x10052",
"$": "PX2 030610"
}
]
},
{
"@mh": "0x115014",
"attribute": [
{
"@id": "0x1006e",
"$": "switch2"
},
{
"@id": "0x23000e",
"$": "DCS-7010T-48"
},
{
"@id": "0x12d7f",
"$": "10.10.10.2"
},
{
"@id": "0x10052",
"$": "Arista Networks EOS version 4.22.3M-INT running on an Arista Networks DCS-7010T-48"
}
]
},Help Iterate Python - Json file
How to iterate through this dictionary (dictionary/list within the dictionary)?
What you're looking at is called 'json'. It's basically xml with javascript friendly syntax.
It's more or less the modern standard for sending data to and from apis. Python has a built in tool to work with it: https://docs.python.org/2/library/json.html
Let's call your data json_raw
import json json_dict = json.loads(json_raw)
From here json_dict is a python dictionary object that represents that data. It uses dict syntax to access the data:
for example: json_dict['teams'] gets the teams json_dict['teams'][0]['_links']['fixtures']['href'] would return "http://api.football-data.org/alpha/teams/66/fixtures"
you can iterate through dictionaries:
for team in json_dict['teams']:
print team['_links']['fixtures']['href']should output each fixtures url
edit:
SEE https://gist.github.com/bionikspoon/d7014741472e43f41a25
I was just playing with it. Something sparked my curiosity. But this works.
More on reddit.compython - Iterating through a JSON object - Stack Overflow
Looping over a nested dictionary (from json file) and creating a new dictionary with some omissions - need help
I've got my dictionary of scraped data here, I was wondering how I can take this and return the data I want from it?
I just want to return the inner dictionary (so 'links, code, cresturl, name, etc') and I'm a bit confused about the structure of this as I haven't found the right question to google...
edit: well this is a stupid question. How do I return the "teams" list/dictionary specifically? It looks like a dictionary within a list because of the syntax [ { } ]
{
"_links": [
{
"self": "http://api.football-data.org/alpha/soccerseasons/354/teams"
},
{
"soccerseason": "http://api.football-data.org/alpha/soccerseasons/354"
}
],
"count": 20,
"teams": [
{
"_links": {
"fixtures": {
"href": "http://api.football-data.org/alpha/teams/66/fixtures"
},
"players": {
"href": "http://api.football-data.org/alpha/teams/66/players"
},
"self": {
"href": "http://api.football-data.org/alpha/teams/66"
}
},
"code": "MUFC",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
"name": "Manchester United FC",
"shortName": "ManU",
"squadMarketValue": "425,000,000 \u20ac"
},
{
"_links": {
"fixtures": {
"href": "http://api.football-data.org/alpha/teams/72/fixtures"
},
"players": {
"href": "http://api.football-data.org/alpha/teams/72/players"
},
"self": {
"href": "http://api.football-data.org/alpha/teams/72"
}
},
"code": "SWA",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/a/ab/Swansea_City_Logo.svg",
"name": "Swansea City",
"shortName": "Swans",
"squadMarketValue": "104,000,000 \u20ac"
},
{
"_links": {
"fixtures": {
"href": "http://api.football-data.org/alpha/teams/338/fixtures"
},
"players": {
"href": "http://api.football-data.org/alpha/teams/338/players"
},
"self": {
"href": "http://api.football-data.org/alpha/teams/338"
}
},
"code": "LCFC",
"crestUrl": "http://upload.wikimedia.org/wikipedia/en/6/63/Leicester02.png",
"name": "Leicester City",
"shortName": "Foxes",
"squadMarketValue": "63,250,000 \u20ac"
},
{
"_links": {
"fixtures": {
"href": "http://api.football-data.org/alpha/teams/62/fixtures"
},
"players": {
"href": "http://api.football-data.org/alpha/teams/62/players"
},
"self": {
"href": "http://api.football-data.org/alpha/teams/62"
}
},
"code": "EFC",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/f/f9/Everton_FC.svg",
"name": "Everton FC",
"shortName": "Everton",
"squadMarketValue": "179,250,000 \u20ac"
},
.... etc
}
]
}What you're looking at is called 'json'. It's basically xml with javascript friendly syntax.
It's more or less the modern standard for sending data to and from apis. Python has a built in tool to work with it: https://docs.python.org/2/library/json.html
Let's call your data json_raw
import json
json_dict = json.loads(json_raw)
From here json_dict is a python dictionary object that represents that data. It uses dict syntax to access the data:
for example: json_dict['teams'] gets the teams json_dict['teams'][0]['_links']['fixtures']['href'] would return "http://api.football-data.org/alpha/teams/66/fixtures"
you can iterate through dictionaries:
for team in json_dict['teams']:
print team['_links']['fixtures']['href']
should output each fixtures url
edit:
SEE https://gist.github.com/bionikspoon/d7014741472e43f41a25
I was just playing with it. Something sparked my curiosity. But this works.
If your outermost dict is called outer, this will iterate on the inner dicts:
for dct in outer["teams"]:
links = dct["_links"]
code = dct["code"]
# etc...
I believe you probably meant:
from __future__ import print_function
for song in json_object:
# now song is a dictionary
for attribute, value in song.items():
print(attribute, value) # example usage
NB: You could use song.iteritems instead of song.items if in Python 2.
Your loading of the JSON data is a little fragile. Instead of:
json_raw= raw.readlines()
json_object = json.loads(json_raw[0])
you should really just do:
json_object = json.load(raw)
You shouldn't think of what you get as a "JSON object". What you have is a list. The list contains two dicts. The dicts contain various key/value pairs, all strings. When you do json_object[0], you're asking for the first dict in the list. When you iterate over that, with for song in json_object[0]:, you iterate over the keys of the dict. Because that's what you get when you iterate over the dict. If you want to access the value associated with the key in that dict, you would use, for example, json_object[0][song].
None of this is specific to JSON. It's just basic Python types, with their basic operations as covered in any tutorial.
Hello fellow Pythonians!
I'm working on repurposing a library and have run into a hitch that I could use some help with.
I've got a json file that I've brought in as a dictionary of dictionaries of dictionaries (so double-nested). I need to loop through the variables, get a list of the keys, then loop through those keys, get the key-value-pairs of the innerstats.
Here's what I have so far (assume the json file is already loaded):
def get_keys(dict):
keys = []
for key in dict.keys():
keys.append(key)
return keys
variable_keys = get_keys(variable_data)Which gets me a list of the outer keys.
Now I need to loop through the outer keys and get the KVPs with some omissions, so I have:
def create_dict():
new_dict = {}
for keys in variable_keys:
new_dict.update({k: v for k, v in variable_data[keys].items() if k not in ['omit1', 'omit2', 'omit3']})
return new_dict
variable_table = create_dict()But this is only returning 1 of the variables and not the full dictionary of variables.
I've been banging my head against this for a couple of hours so any help would be appreciated.
My Json is of the following format:
https://pastebin.com/KUKHHh2e
The numbers in this json are many as are the dates. I am trying to iterate through this to create a single list of dictionaries that contains all of the information in the dictionary that is 3 layers deep in the json.
I have made a loop that is n3 but it seems highly inefficient given i have around 30,000 iterations to make.
What are my options here?