How to parse values from a JSON file in Python - python

I'm trying to get the values from the json file and the error that I'm getting is TypeError: expected string or buffer. I'm parsing the file correctly and moreover I guess my json file format is also correct. Where I'm going wrong?
Both the files are in the same directory.
Main_file.py
import json
json_data = open('meters_parameters.json')
data = json.loads(json_data) // TypeError: expected string or buffer
print data
json_data.close()
meters_parameters.json
{
"cilantro" : [{
"cem_093":[{
"kwh":{"function":"3","address":"286","length":"2"},
"serial_number":{"function":"3","address":"298","length":"2"},
"slave_id":{"function":"3","address":"15","length":"2"}
}]
}],
"elmeasure" : [{
"lg1119_d":[{
"kwh":{"function":"3","address":"286","length":"2"},
"serial_number":{"function":"3","address":"298","length":"2"},
"slave_id":{"function":"3","address":"15","length":"2"}
}]
}]
}

loads expects a string not a file handle. You need json.load:
import json
with open('meters_parameters.json') as f:
data = json.load(f)
print data

You're trying to load the file object, when you want to load everything in the file. Do:
data = json.loads(json_data.read())
.read() gets everything from the file and returns it as a string.
A with statement is much more pythonic here as well:
with open('meters_parameters.json') as myfile:
data = json.loads(myfile.read())

Related

Opening a json file from computer as a dictionary

I wrote the following function that I want to apply to a json file:
import json
def myfunction(dictionary):
#doing things
return new_dictionary
data = """{
#a json file as a dictionary
}"""
info = json.loads(data)
refined = key_replacer(info)
new_data = json.dumps(refined)
print(new_data)
It works fine, but how do I do it when I want to import a file from my computer? json.loads take a string as input and returns a dictionary as output and json.dumps take a dictionary as input and returns a string as output. I tried with:
with open('C:\\Users\\SONY\\Desktop\\test.json', 'r', encoding="utf8") as data:
info = json.loads(data)
But TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper.
You are passing a file object instead of string. To fix that, you need to read the file first json.loads(data.read())
Howerver, you can directly load json from files using json.load(open('myFile','r')) or in your case, json.load(data)
loads and dumps work on strings. If you want to work on files you should use load and dump instead.
Here is an example:
from json import dump, load
with open('myfile.json','r') as my_file:
content = load(my_file)
#do stuff on content
with open('myooutput.json','w') as my_output:
dump(content, my_output)

How to iterate through dictionary shape .txt file to create DataFrame

I have .txt file with a dictionary inside, like (several rows):
{
"data": [
{
"title": "Greatest chess game",
"created_time": "2020-02-17T16:51:44+0000"
}
]
}
I need to open this file and create df look alike:
title created_time
0 Greatest chess game 2020-02-17T16:51:44+0000
....
When I open txt file:
output_file=open('data\\data_new.txt', 'w')
with open(output_file, 'r') as reader:
print(reader.readline(5))
It treats as a:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
How could I convert it ?
Not sure why you are opening the file twice, once in read mode and once in write, not sure that's actually possible.
Anyway, as mentioned what you appear to have is JSON so use the json library to read the file.
import json
import pandas as pd
with open('data_new.txt', 'r', encoding='utf-8') as reader:
json = json.load(reader )
df = pd.DataFrame(json['data'])
print(df)
I think you could use json lib to open your text file as json file, like:
import json
with open('data.txt') as json_file:
data = json.load(json_file)
#here you can make you dataframe

How to handle the json convert object in python

I have a JSON file which I converted in the python object using json.load() function.I want the output to be dict or list but its a string.
PS: The data I could not share because its a production data.
Thanks in advance:)
you mean you have file is myJsonfile.json right ?
and you want to load them into python?
you can using like this
import json
file = open('myjsonfile.json','r')
jsonfile = json.load(file)
to check data it's was python now call
jsonfile
to change from a string to dict/list use json.loads()
So what every your stored string is, in the example below I called is jsonStr:
jsonStr = '''{"file_type": "json" , "data_type": "str"}'''
jsonObj = json.loads(jsonStr)
Output:
print (jsonObj)
{'file_type': 'json', 'data_type': 'str'}
print (type(jsonObj))
<class 'dict'>

Error loading json in python line by line?

Here is my json file format,
[{
"name": "",
"official_name_en": "Channel Islands",
"official_name_fr": "Îles Anglo-Normandes",
}, and so on......
while loading the above json which is in a file I get this error,
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes:
here is my python code,
import json
data = []
with open('file') as f:
for line in f:
data.append(json.loads(line))
,} is not allowed in JSON (I guess that's the problem according to the data given).
You appear to be processing the entire file one line at a time. Why not simply use .read() to get the entire contents at once, then feed that to json?
with open('file') as f:
contents = f.read()
data = json.loads(contents)
Better yet, why not use json.load() to pass the readable directly and let it handle the slurping?
with open('file') as f:
data = json.load(f)
The problem is in your reading and decoding the file line by line. Any single line in your file (e.g., "[{") is not a valid JSON expression.
Your individual lines are not valid JSON. For instance, the first line '[{' by itself is not a valid JSON. If your entire file is actually valid JSON and you want individual lines, first load the entire JSON and then browse through the python dictionary.
import json
data = json.loads(open('file').read()) # this should be a list
for list_item in data:
print(list_item['name'])

Valid JSON in text file but python json.loads gives "JSON object could be decoded"

I have a valid JSON (checked using Lint) in a text file.
I am loading the json as follows
test_data_file = open('jsonfile', "r")
json_str = test_data_file.read()
the_json = json.loads(json_str)
I have verified the json data in file on Lint and it shows it as valid. However the json.loads throws
ValueError: No JSON object could be decoded
I am a newbie to Python so not sure how to do it the right way. Please help
(I assume it has something to do it encoding the string to unicode format from utf-8 as the data in file is retrieved as a string)
I tried with open('jsonfile', 'r') and it works now.
Also I did the following on the file
json_newfile = open('json_newfile', 'w')
json_oldfile = open('json_oldfile', 'r')
old_data = json_oldfile.read()
json.dump(old_data, json_newfile)
and now I am reading the new file.

Categories

Resources