I started learning to code recently, and I had a query about some for loop syntax in python. I've been having a look at the NPR API module on codecademy (which, I realize, is not a great environment for learning anything) and the way a for loop is presented has me confused. The part in question:
from urllib2 import urlopen
from json import load
url = "http://api.npr.org/query?apiKey="
key = "API_KEY"
url += key
url += "&numResults=3&format=json&id="
npr_id = raw_input("Which NPR ID do you want to query?")
url += npr_id
print url
response = urlopen(url)
json_obj = load(response)
for story in json_obj["list"]["story"]:
print story["title"]["$text"]
I'm confused about the
for story in json_obj["list"]["story"]:
print story["title"]["$text"]
lines. Is it some kind of nested list?
Think of a json object as a dictionary.
The square bracket notation is how the json object is accessed.
Basically json_obj["list"]["story"] is a nested dictionary with an array of dictionaries and it would make more sense if the key name was json_obj["list"]["stories"].
The json_obj has a key "list" and the value of json_obj["list"] has a key of "story" and each story has a "title".
There is an example here of parsing json: Parsing values from a JSON file using Python?
Here is the structure of what the json object would look like based on how you have written it:
json_obj = {
'list': {
# this is the array that is being iterated
'story': [
{'title': {
'$text': 'some title1'
}
},
{'title': {
'$text': 'some title2'
}
},
{'title': {
'$text': 'some title3'
}
},
],
},
}
So the for loop:
for story in json_obj["list"]["story"]:
# each iteration story become this
# story = {'title': {'$text': 'some title2'}}
print story["title"]["$text"]
Which is similar to:
print json_obj['list']['story'][0]['title']['$text']
print json_obj['list']['story'][1]['title']['$text']
print json_obj['list']['story'][2]['title']['$text']
Related
{ "PrcCfgDetails": [
{
"header_id": "10984299",
"line_id": "1143673632"
},
{
"header_id": "10984299",
"line_id": "1143673633"
}
]
}
I have above response file and I have written next 2 line of code in python. I do see above in response2 but don't know how to write a loop and get the value of header_id and line_id for number of records in response. Can you please help me in writing rest of the code.
response2 = requests.post(OAUTH_ENDPOINT, headers=headers_get, data=python2json)
user_data1 = json.dumps(response2.text)
user_data1 = response2.json()
Will return a dictionary of lists of dictionary. The top level will have one key -- PrcCfgDetails -- accessible with user_data1.get('ProCfgDetails')
I have a json that I have via a python request, and I need to check if the "extension" key is equal to "txt", if so give a message.
from requests import get
import json
url = get('http://', auth = ('user', 'password'))
data_JSON = url.json()
for element in json['diffs']:
if element['extension'] == 'txt':
print ("have txt")
The json that returns from the request is this:
{
"fromHash":"***",
"toHash":"BG",
"contextLines":10,
"whitespace":"SHOW",
"diffs":[
{
"source":{
"components":[
"arquivo1.txt"
],
"parent":"",
"name":"arquivo1.txt",
"extension":"txt",
"toString":"arquivo1.txt"
},
....
I want to check the extension key if the value is txt, if yes return a message
Use element['source']['extension'] instead of element['extension'] since extension is a key within element['source'] not element directly.
from requests import get
import json
url = get('http://', auth = ('user', 'password'))
data_JSON = url.json()
for element in data_JSON['diffs']:
if element['source']['extension'] == 'txt':
print('have txt')
There is no need to use a for loop to iterate over "diffs".
Simply use an index such as 0.
Take a look at the following data which represents your JSON structure.
sample_json = {
"diffs":[
{
"source":{
"extension":"txt",
}
}
]
}
It appears, our first key is called diffs which has a list value.
We can easily access the diffs list by using sample_json["diffs"]; This returns a list.
So we index it -> sample_json["diffs"][0]
We get a dictionary (go to source) -> sample_json["diffs"][0]["source"]
Look for the extensions key -> sample_json["diffs"][0]["source"]["extensions"]
I've given you an explanation so that later on in the future you can follow a similar process.
code
sample_json = {
"diffs":[
{
"source":{
"extension":"txt",
}
}
]
}
extension = sample_json["diffs"][0]["source"]["extension"]
if extension == "txt":
print("true")
I'm working in Python (3.8) and I've successfully called an API gotten it to print the JSON within command line after running the Python file. Now, I want to be able to print a particular list of information (like all of the names from the JSON), and later on save that list as its own set of data, but I'm hitting a block.
Example JSON I'm working with:
{
"data": {
"employees": [
{
"fields": {
"name": "Buddy",
"superheroName": "Syndrome",
"workEmail": "syndrome#example.com",
}
},
{
"fields": {
"name": "Helen Parr",
"superheroName": "Elastigirl",
"workEmail": "elastigirl#example.com",
}
}
]
}
I’ve tried the following so far and I was able to get “data” to print, but anytime I try to print another “layer” and get to say...“employees” or “fields” even, I hit a wall.
url = "my API url"
response = requests.get(url)
if response.status_code != 200:
print('Error with status code {}'.format(response.status_code))
exit()
jsonResponse = response.json()
jsonPretty = json.dumps(jsonResponse, indent=4, sort_keys=True)
jsonDictionary = json.loads(jsonPretty)
keys = jsonDictionary.keys()
for key in jsonDictionary.keys():
print(key)
Ideally, could someone share insight into how I can access the 'name' JSON value and get Python to print it as a list like the following, for example:
Buddy
Helen Parr
JSON files are basically nested dictionaries. jsonDictionary only contains one key and one entry under that key: data and another dictionary with the rest your result respectively.
If you wanted to access the name fields specifically:
employeesDict = jsonDictionary['data']
feildsDictList = employeesDict['employees']
firstFieldsDict = fieldsDictList[0]
secondFieldsDict = fieldsDictList[1]
firstName = firstFieldsDict['name']
secondNAme = secondFieldsDict['name']
You can access it like this (make sure it's already a dictionary):
for i in h['data']['employees']:
print(i['fields']['name'])
This way you can access the names with i['fields']['name']
I have the following json object (Say car_details.json):
{
"name":"John",
"age":30,
"cars":
[
{
"car_model": "Mustang",
"car_brand": "Ford"
},
{
"car_model": "cx-5",
"car_brand": "Mazda"
}
}
I want to change the value of car_model from cx-5 to cx-9 through python code.
I am providing the json path to this element, through an external file. The json-path expression is basically represented as a string. Something like this:
'cars[2].car_model'
And the new value is also provided through an external file as a string:
'cx-9'
Now how do I parse through car_details.json using the jsonpath expression, and change its value to the one provided as string, and finally return the modified json object
P.S I want to do this through python code
This is an approach without using json module. Load your data in variable. Then iterate over cars key/values. If you find the key that is the value you are looking for set it to new value.
Also note: you need to close your array block, otherwise your above json is not valid. Generally I use an online json parser to check if my data is valid etc. (may be helpful in future).
data = {
"name":"John",
"age":30,
"cars":
[
{
"car_model": "Mustang",
"car_brand": "Ford"
},
{
"car_model": "cx-5",
"car_brand": "Mazda"
}
]
}
for cars in data['cars']:
for key, value in cars.items():
if key == "car_model" and value == "cx-5":
cars[key] = "cx-9"
print(data)
If you want to load your json object from a file, let's assume it is called "data.json" and is in the same directory as the python script you are going to run:
import json
with open('data.json') as json_data:
data = json.load(json_data)
for cars in data['cars']:
for key, value in cars.items():
if key == "car_model" and value == "cx-5":
cars[key] = "cx-9"
print(data)
Now if you'd like to write the content to the original file or new file, in this case I am writing to a file called "newdata.json":
import json
import re
with open('data.json') as json_data:
data = json.load(json_data)
print(data)
with open('external.txt') as f:
content = f.read()
print(content)
for cars in data['cars']:
for key, value in cars.items():
if key == "car_model" and value == "cx-5":
cars[key] = content
with open('newdata.json', 'w') as outfile:
json.dump(data, outfile)
I'm trying to append an existing JSON file. When I overwrite the entire JSON file then everything works perfect. The problem that I have been unable to resolve is in the append. I'm completely at a loss at this point.
{
"hashlist": {
"QmVZATT8jWo6ncQM3kwBrGXBjuKfifvrE": {
"description": "Test Video",
"url": ""
},
"QmVqpEomPZU8cpNezxZHG2oc3xQi61P2n": {
"description": "Cat Photo",
"url": ""
},
"QmYdWb4CdFqWGYnPA7V12bX7hf2zxv64AG": {
"description": "test.co",
"url": ""
}
}
}%
Here is the code that I'm using where data['hashlist'].append(entry) receive AttributeError: 'dict' object has no attribute 'append'
#!/usr/bin/python
import json
import os
data = []
if os.stat("hash.json").st_size != 0 :
file = open('hash.json', 'r')
data = json.load(file)
# print(data)
choice = raw_input("What do you want to do? \n a)Add a new IPFS hash\n s)Seach stored hashes\n >>")
if choice == 'a':
# Add a new hash.
description = raw_input('Enter hash description: ')
new_hash_val = raw_input('Enter IPFS hash: ')
new_url_val = raw_input('Enter URL: ')
entry = {new_hash_val: {'description': description, 'url': new_url_val}}
# search existing hash listings here
if new_hash_val not in data['hashlist']:
# append JSON file with new entry
# print entry
# data['hashlist'] = dict(entry) #overwrites whole JSON file
data['hashlist'].append(entry)
file = open('hash.json', 'w')
json.dump(data, file, sort_keys = True, indent = 4, ensure_ascii = False)
file.close()
print('IPFS Hash Added.')
pass
else:
print('Hash exist!')
Usually python errors are pretty self-explanatory, and this is a perfect example. Dictionaries in Python do not have an append method. There are two ways of adding to dictionaries, either by nameing a new key, value pair or passing an iterable with key, value pairs to dictionary.update(). In your code you could do:
data['hashlist'][new_hash_val] = {'description': description, 'url': new_url_val}
or:
data['hashlist'].update({new_hash_val: {'description': description, 'url': new_url_val}})
The first one is probably superior for what you are trying to do, because the second one is more for when you are trying to add lots of key, value pairs.
You can read more about dictionaries in Python here.