Parse JSON starting with bracket in python - python

I have a .json file structured as well:
"[{\"dataset\": \"x0\", \"test\": \"Test 3 \", \"results\": {\"TP\": 0, \"FP\": 0, \"FN\": 0, \"TN\": 17536}, \"dir\": \"/Users//Test_3\"}]"
When I try to read it with the following code:
with open(dir, 'r+') as f:
data = json.load(f)
print(data[0])
I get [ as output, which means it is reading the json object as a string.
I do not understand if the problem is how I'm saving it. Since I populate it in a loop, the code which creates this object is the following one:
json_obj = []
for i in range(len(dictionary)):
dataset, test, dir = retrieve_data()
tp, fp, tn, fn = calculate_score()
json_obj.append({'dataset': dataset,
'test': test,
'results': {'TP': tp, 'FP': fp, 'FN': fn, 'TN': tn},
'dir': dir })
json_dump = json.dumps(json_obj)
with open(save_folder, 'w') as outfile:
json.dump(json_dump, outfile)
The structure I tried to create is the following one:
{
"dataset": "1",
"test": "trial1",
"results": {
"TP": 5,
"FP": 3,
"FN": 2,
"TN": 5
},
"dir": dir
}
How can I read it correctly to make it parsable?

You are converting json_obj to a string and then dumping the string to a file. Dump json_obj directly to the file:
#json_dump = json.dumps(json_obj)
with open(save_folder, 'w') as outfile:
json.dump(json_obj, outfile)

Related

find and replace specific json data in python?

I am trying to replace data1 with data5 in json body of Full Address, but my code is giving error with these (list indices must be integers or slices, not str).
Please see my code as well as json body below.
json
[
{
"fields": {
"Full Address": "data1",
"pl/p/0": "212152",
"ot": "fgde"
}
},
{
"fields": {
"Full Address": "data2",
"pl/p/0": "52562",
"ot": "frtfr"
}
}
]
code
import json
with open('jsonbody.json') as f:
data = json.load(f)
for item in data['fields']:
item['Full Address'] = item['Full Address'].replace('data1', 'data5')
with open('new_data.json', 'w') as f:
json.dump(data, f)
the data is list, and its element is a dict, which contains fields key
import json
with open('jsonbody.json') as f:
data = json.load(f)
for item in data:
item['fields']['Full Address'] = item['fields']['Full Address'].replace('data1', 'data5')
with open('new_data.json', 'w') as f:
json.dump(data, f)
Here I am answering my own question with adding german umlaut detail.
import json
jsonbody = 'jsonbody.encode()'
with open('jsonbody.json', 'r') as file:
json_data = json.load(file)
for item in json_data:
if item['fields']['Full Address'] in ["data1"]:
item['fields']['Full Address'] = "data5_Ä"
with open('new_data.json', 'w') as file:
json.dump(json_data, file, indent=2)
# if my detail contain german words then I will use this
json.dump(json_data, file, indent=2, ensure_ascii=False)

Python program to produce dictionary of file names, extensions, path, creation times, and sizes, and output them to a json file

import os, json, time
path = "C:\\Users\\Marius\\Desktop\\homework" #se defineste adresa
with os.scandir(path) as listOfEntries:
for item in listOfEntries:
data={}
if item.is_file():
filename_ext=os.path.splitext(item)
size=(os.path.getsize(item))
creation=(time.ctime(os.path.getctime(item)))
extension=(os.path.splitext(os.path.basename(item))[1])
if filename_ext not in data:
data[item.path] = {'name': item.name, 'path': path, 'extension': extension, 'creation': creation, 'size': size}
print(data)
j_data = json.dumps(data, indent=4)
with open('files.json', 'w') as f:
json.dump(data, f, indent=4)
I cannot figure it out. Any help would be appreciated.
I tried to make a python file to generate the results needed, but I need them to be as an output JSON file.
I want the results to be as an output to files.JSON, something like the following result, but with all the files from that specific folder:
{
"C:\\Users\\Marius\\Desktop\\homework\\test.py": {
"name": "test.py",
"path": "C:\\Users\\Marius\\Desktop\\homework",
"extension": ".py",
"creation": "Sat Dec 26 08:39:59 2020",
"size": 733
}
}
Solved with:
import os, json, time
path = "C:\\Users\\Marius\\Desktop\\homework"
data={}
with os.scandir(path) as listOfEntries:
for item in listOfEntries:
filename_ext=os.path.splitext(item)
size=(os.path.getsize(item))
creation=(time.ctime(os.path.getctime(item)))
extension=(os.path.splitext(os.path.basename(item))[1])
if item.is_file():
if filename_ext not in data:
data[item.path] = {'name': item.name, 'path': path, 'extension': extension, 'creation': creation, 'size': size}
print(data)
j_data = json.dumps(data, indent=4)
with open('files.json', 'w') as f:
json.dump(data, f, indent=4)

Trouble with this error while adding a dict to an existing key value

Code Including JSON File Code:
Python( Suppose To Append A New User and Balance):
import json
with open('users_balance.json', 'r') as file:
data = json.load(file)['user_list']
data['user_list']
data.append({"user": "sdfsd", "balance": 40323420})
with open('users_balance.json', 'w') as file:
json.dump(data, file, indent=2)
Json(Object The Code Is Appending To):
{
"user_list": [
{
"user": "<#!672986823185661955>",
"balance": 400
},
{
"user": "<#!737747404048171043>",
"balance": 500
}
],
}
Error(Traceback Error Given After Executing Code):
data = json.load(file)['user_list']
KeyError: 'user_list'
The solution is this:
import json
with open('users_balance.json', 'r') as file:
data = json.load(file)
data['user_list'].append({"user": "sdfsd", "balance": 40323420})
with open('users_balance.json', 'w') as file:
json.dump(data, file, indent=2)

How to append data properly to a JSON using python

Update:
The only issue I have now is when running the command to add a user it create a completely duplicate key.
Question:
json.dump() simply adds the entry to the end of the json, I want it to overwrite the entire file with the new updated entry
Setup: (Create blank "Banks" Field)
with open(DATA_FILENAME, mode='w', encoding='utf-8') as f:
data = {"banks": []}
json.dump(data, f)
Set User: (Create a User Key inside "Banks")
member = ctx.message.author
entry = {'name': member.name, 'id': member.id, 'balance': 0}
with open(DATA_FILENAME, 'r+') as outfile:
data = json.load(outfile)
data['banks'].append((entry))
json.dump(data, outfile, indent=4)
Output of first use:
{"banks": []}{
"banks": [
{
"name": "ViperZ-14",
"id": 367151547575959562,
"balance": 0
}
]
}
What I need:
{
"banks": [
{
"name": "ViperZ-14",
"id": 367151547575959562,
"balance": 0
}
]
}
file_path = '/home/vishnudev/Downloads/new.json'
import json
def load(file, mode, data=[]):
with open(file, mode) as f:
if mode == 'r':
return json.load(f)
elif mode == 'w':
json.dump(data, f)
def get_data_func():
return {
'name': 'vishnu',
'data': 'dev'
}
d = load(file_path, 'r')
print(d)
d.append(get_data_func())
load(file_path, 'w', d)
d = load(file_path, 'r')
print(d)
Output:
On running the above twice I get
[{'name': 'vishnu', 'data': 'dev'}]
[{'name': 'vishnu', 'data': 'dev'}, {'name': 'vishnu', 'data': 'dev'}]
I have found that the solution was to simply seek to the beginning of the document. The json.dump() does overwrite but it only overwrites whats in its way. AKA, seeking/placing the cursor at the top of the document will overwrite the entire document using the new entry.

JSON dump breaks my dictionary when using certain characters

Dumping data into a file with json.dump.
The data looks like this:
{"hello": {"this": 1, "a": 1, "is": 1, "test": 1}}
The code I use to achieve this is as follows (worddict is a file, something like file.json):
with open(words, 'w') as fp:
json.dump(worddict, fp)
fp.close()
I'd like to have the data in this format:
{
"hello": {
"a": 1,
"is": 1,
"test": 1,
"this": 1
}
I changed the code to this:
with open(words, 'w') as fp:
json.dump(worddict, fp, sort_keys=True, indent=4, separators=(',', ': '))
fp.close()
And it works, until I try to dump characters "Á", "É", "Ű"...
These characters breaks the worddict file, and when I cat the file it looks like this:
{
Any idea why?
Replace
json.dump(worddict, fp, sort_keys=True, indent=4, separators=(',', ': '))
with
json.dump(worddict, fp, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)
I ran your code snippet in both python2 and python3.
In python3, it gave me no errors.
I ran the following code:
import json
words = 'a.txt'
worddict = {"hello": {"this": 1, "a": 1, "is": 1, "test": "Á"}}
with open(words, 'w') as fp:
json.dump(worddict, fp, sort_keys=True, indent=4, separators=(',', ': '))
and got as output:
{
"hello": {
"a": 1,
"is": 1,
"test": "\u00c1",
"this": 1
}
}
But in python2, I ran into errors. I got a link describing the error:
http://www.python.org/peps/pep-0263.html
The problem occurs in python2 as python2 strings as not unicode by default. So you have to mention encoding at the top of the source code file.
I added "# coding=UTF-8" to the top of the file, before the python source code starts, so as to let python interpreter know the encoding of the file. Once I did that, the code ran in python2 as well as python3 with no errors.
I got the following as output:
{
"hello": {
"a": 1,
"is": 1,
"test": "\u00c1",
"this": 1
}
}
Here is my full final source code that I used.
# coding=UTF-8
import json
words = 'a.txt'
worddict = {"hello": {"this": 1, "a": 1, "is": 1, "test": "Á"}}
with open(words, 'w') as fp:
json.dump(worddict, fp, sort_keys=True, indent=4, separators=(',', ': '))

Categories