how do i parse json file using python - python

After doing a rest api call and storing result as json file contents of json file look as follows:
["x","y","z"]
I need to use python script to iterate through each item and print it out.
I have the following snippet of code which does error out.
with open('%s/staging_area/get_label.json' % cwd) as data_file:
data = json.load(data_file)
for item in data:
print data [item]
Error I am getting is as follows:
Traceback (most recent call last):
File "Untitled 8.py", line 33, in <module>
print data [item]
TypeError: list indices must be integers, not unicode
What am I missing? Thank you for your help!

In the line
for item in data:
you set item to be an element of data, but then in the line
print data [item]
you use item as an index, which it is not. Hence the error. There is also no need to use an index since item is already an element of data.
What you can do instead is:
for item in data:
print(item)

Related

Python how to parse a json usng tags in a loop

I have a list of 155 json tags and a json file associated with it. The idea is to parse the json file using the predetermined tags and if one of the tags is not available in the json string then use null.
I am using python 2.7 and native json parser. I need to convert the json in to csv. The problem here is am trying to extract json fields using tag from a file but I am having issues evaluating the tags. I am getting a key error.
I have a list of json schema like below.
json Schema file:
jsonTag.tagb,csv_columnA
jsonTag2.Id,csv_columnB
jsonTag2.Id.description,csv_columnC
I want to loop through the above and get all the json tags.
Use the json tag to extract from the json file. the above is just an example we have around 155 tags and complext nested json.
Below is the method I have in python which gets a schema (tags) from a different method using an ordered Dict format which I need to use to extract the json
def write_to_csv(self):
myvars = self.generate_schema() #gets schema in orderedDict key value parir
header = []
values = []
with open('sample_json.json','r') as json_file: #read json file row by row json string
json_string = json_file.read().splitlines()
for json_line in json_string:
json_parsed=json.loads(json_line) #parse each row
for key, value in myvars.items():
values.append(json_parsed[value]) #evalueat the tag and append to an array which i will use it to do csv write row.
print values
Below is the error i am getting now.
OUTPUT:
Traceback (most recent call last):
File "./parse_nested_json.py", line 86, in <module>
main()
File "./parse_nested_json.py", line 83, in main
parse.write_to_csv()
File "./parse_nested_json.py", line 63, in write_to_csv
values.append(json_parsed[value.strip('\"')])
KeyError: "['trackingEvent']['barcode']"
Expected output:
csv
csv_columnA,csv_columB,csv_columnC
1223,abc,aaa
234,asd,ssaa
..etc
Thanks in advance

create valid json object in python

Each line is valid JSON, but I need the file as a whole to be valid JSON.
I have some data which is aggregated from a web service and dumped to a file, so it's JSON-eaque, but not valid JSON, so it can't be processed in the simple and intuitive way that JSON files can - thereby consituting a major pain in the neck, it looks (more or less) like this:
{"record":"value0","block":"0x79"}
{"record":"value1","block":"0x80"}
I've been trying to reinterpret it as valid JSON, my latest attempt looks like this:
with open('toy.json') as inpt:
lines = []
for line in inpt:
if line.startswith('{'): # block starts
lines.append(line)
However, as you can likely deduce by the fact that I'm posing this question- that doesn't work- any ideas about how I might tackle this problem?
EDIT:
Tried this:
with open('toy_two.json', 'rb') as inpt:
lines = [json.loads(line) for line in inpt]
print(lines['record'])
but got the following error:
Traceback (most recent call last):
File "json-ifier.py", line 38, in <module>
print(lines['record'])
TypeError: list indices must be integers, not str
Ideally I'd like to interact with it as I can with normal JSON, i.e. data['value']
EDIT II
with open('transactions000000000029.json', 'rb') as inpt:
lines = [json.loads(line) for line in inpt]
for line in lines:
records = [item['hash'] for item in lines]
for item in records:
print item
This looks like NDJSON that I've been working with recently. The specification is here and I'm not sure of its usefulness. Does the following work?
with open('the file.json', 'rb') as infile:
data = infile.readlines()
data = [json.loads(item.replace('\n', '')) for item in data]
This should give you a list of dictionaries.
Each line looks like a valid JSON document.
That's "JSON Lines" format (http://jsonlines.org/)
Try to process each line independantly (json.loads(line)) or use a specialized library (https://jsonlines.readthedocs.io/en/latest/).
def process(oneline):
# do what you want with each line
print(oneline['record'])
with open('toy_two.json', 'rb') as inpt:
for line in inpt:
process(json.loads(line))

Getting error while creating multiple file in python

I'm creating two files using python script, first file is JSON and second one is HTML file, my below is creating json file but while creating HTML file I'm getting error. Could someone help me to resolve the issue? I'm new to Python script so it would be really appreciated if you could suggest some solution
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import json
JsonResponse = '[{"status": "active", "due_date": null, "group": "later", "task_id": 73286}]'
def create(JsonResponse):
print JsonResponse
print 'creating new file'
try:
jsonFile = 'testFile.json'
file = open(jsonFile, 'w')
file.write(JsonResponse)
file.close()
with open('testFile.json') as json_data:
infoFromJson = json.load(json_data)
print infoFromJson
htmlReportFile = 'Report.html'
htmlfile = open(htmlReportFile, 'w')
htmlfile.write(infoFromJson)
htmlfile.close()
except:
print 'error occured'
sys.exit(0)
create(JsonResponse)
I used below online Python editor to execute my code:
https://www.tutorialspoint.com/execute_python_online.php
infoFromJson = json.load(json_data)
Here, json.load() will expect a valid json data as json_data. But the json_data you provided are not valid json, it's a simple string(Hello World!). So, you are getting the error.
ValueError: No JSON object could be decoded
Update:
In your code you should get the error:
TypeError: expected a character buffer object
That's because, the content you are writing to the file needs to be string, but in place of that, you have a list of dictionary.
Two way to solve this. Replace the line:
htmlfile.write(infoFromJson)
To either this:
htmlfile.write(str(infoFromJson))
To make infoFromJson a string.
Or use the dump utility of json module:
json.dump(infoFromJson, json_data)
If you delete Try...except statement, you will see errors below:
Traceback (most recent call last):
File "/Volumes/Ithink/wechatProjects/django_wx_joyme/app/test.py", line 26, in <module>
create(JsonResponse)
File "/Volumes/Ithink/wechatProjects/django_wx_joyme/app/test.py", line 22, in create
htmlfile.write(infoFromJson)
TypeError: expected a string or other character buffer object
Errors occurred because htmlfile.write need string type ,but infoFromJson is a list .
So,change htmlfile.write(infoFromJson) to htmlfile.write(str(infoFromJson)) will avoid errors!

Reading a JSON string in Python: Receiving error " TypeError: string indices must be integers"

I am trying to create a program that will get me current weather using the OpenWeatherMap API. I am new to coding in the sense of coding while receiving the data from the internet.
The error I receive is:
"Traceback (most recent call last):
File "/home/pi/Python Codes/Weather/CurrentTest3.py", line 7, in
temp_k = [record['temp'] for record in url2 ['main']] #this line should take down the temperature information from the .Json file
File "/home/pi/Python Codes/Weather/CurrentTest3.py", line 7, in
temp_k = [record['temp'] for record in url2 ['main']] #this line should take down the temperature information from the .Json file
TypeError: string indices must be integers
I do not understand why I am getting this, my code is below.
from dateutil import parser #imports parser
from pprint import pprint #imports pprint
import requests #imports request
url = requests.get('http://api.openweathermap.org/data/2.5/weather? q=london&APPID=APIKEY') #identifies the url address
pprint(url.json()) #prints .JSON information from address
url2 = url.json() #establishes .Json file as variable
temp_k = [record['temp'] for record in url2 ['main']] #this line should take down the temperature information from the .Json file
print(temp_k) #prints the value for temperature
The value for main in your data is a dict, not a list of dicts. So there is no need to iterate through it; just access the temp value directly.
temp_k = url2['main']['temp']
The problem is with this portion of temp_k, record['temp'].
This is the format of each variable record:
for record in url2 ['main']:
print record
>> pressure
temp_min
temp_max
temp
humidity
It is a bunch of strings that you are trying to index as a dictionary, hence the string indices error. Just change the temp_k line to this:
temp_k = [url2['main'].get('temp')]
>> [272.9]

Parsing data from JSON with python

I'm just starting out with Python and here is what I'm trying to do. I want to access Bing's API to get the picture of the day's url. I can import the json file fine but then I can't parse the data to extract the picture's url.
Here is my python script:
import urllib, json
url = "http://www.bing.com/HPImageArchive.aspx? format=js&idx=0&n=1&mkt=en-US"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data
print data["images"][3]["url"]
I get this error:
Traceback (most recent call last):
File "/Users/Robin/PycharmProjects/predictit/api.py", line 9, in <module>
print data["images"][3]["url"]
IndexError: list index out of range
FYI, here is what the JSON file looks like:
http://jsonviewer.stack.hu/#http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US
print data["images"][0]["url"]
there is only one object in "images" array
Since there is only one element in the images list, you should have data['images'][0]['url'].
You can also see that under the "Viewer" tab in the "json viewer" that you linked to.

Categories