I'm trying to search a JSON file for the username but it is just returning [] rather than tom123.
JSON file contents:
[{"id":"001788fffe48cbdb","username":"tom123"}]
Code:
import json
import re
import requests
f = open("first.json", "r+")
print(f.read())
username = [index["username"] for index in f.read()]
print(username)
f.close()
maybe something like this you want to parse the json so you can use it like a dict
import json
import re
import requests
f = open("first.json", "r+")
data = json.loads(f.read())
username = [index["username"] for index in data]
print(username)
f.close()
f is a file object which is a iterator-like object, which means that when you iterate over it you've consumed it and you cannot use it again. And in this case you consume it first in following line:
print(f.read())
Also for loading a json file you should use json.load() function. Then you can preserve the content after reading then search though the preserved content:
with open("first.json") as f
content = json.load(f)
username = [index["username"] for index in content]
print(username)
Also as a functional-based approach you can use operator.itemgetter and map() in order to get an iterator contain all the usernames (which is more optimized in terms of memory use):
from operator import itemgetter
with open("first.json") as f
content = json.load(f)
usernames = map(itemgetter("username"), content)
Related
I have a json file, and I need to read all of that json file content as String data. How can I read all the data and set a variable as a String for all of that content? Json file has blanks, new lines, special characters etc if it's neccesarry.
Thanks for your help!
import json
from ast import literal_eval
with open('<path_to_json_data>/json_data.txt') as f:
json_data = json.load(f) # dict object
print(json_data, type(json_data))
json_data_as_str = str(json_data) # dict-->str object
print(json_data_as_str, type(json_data_as_str))
data = literal_eval(json_data_as_str) # str-->dict object again
print(data, type(data))
Hope it helps
Simple as this example
import json
with open("path/to/json/filename.json", "r") as json_file:
data = json.load(json_file)
print(data)
dataStr = json.dumps(data)
print(dataStr)
use json.loads
import json
with open(file_name, "r") as fp:
as_string = str(json.loads(fp.read()))
I am able to do with two files, I am not sure if I can use any library to find common data in multiple json file.
import json
with open("data/file1.json", "r") as f1:
file1 = json.loads(f1.read())
with open("data/file2.json", "r") as f2:
file2 = json.loads(f2.read())
for item in file2:
if item in file1:
print(f"Found common: {item}")
file1.append(item)
print(f"New file1: {file1}")
Shared Items
def shared_items(dict_1, dict_2):
return {
key: dict_1[key]
for key in dict_1
if key in dict_2 and \
dict_1[key] == dict_2[key]
}
Your Code
import json
with open("data/file1.json", "r") as f1:
file1 = json.loads(f1.read())
with open("data/file2.json", "r") as f2:
file2 = json.loads(f2.read())
# Load Function
with open("data/shared.json", "w", encoding="utf-8") as file:
shared = json.dumps(shared_items(file1, file2), indent=2, ensure_ascii=False, sort_keys=False)
file.write(shared)
print(f"{shared}\n\n\tdata/shared.json Saved!")
Differences
DeepDiff
This is where deepdiff comes in handy. Deepdiff is a powerful python library to compare 2 dictionaries. What makes it powerful is that, during the comparison, deepdiff does not consider the order in which the elements inside the dictionaries are present. Hence, solves the problem.
Let’s see deepdiff in action
# Load Datas
from json import loads
with open("OLD.json", "r") as file:
old_json = loads(file.read()) # load from old data
with open("NEW.json", "r") as file:
new_json = loads(file.read()) # load from new data
# Magic Import
from deepdiff import DeepDiff
differences = DeepDiff(old_json, new_json, ignore_order=True) # compare the dictionaries
## if there is any difference, the diff will not be empty.
# Pretty Print
from rich import print
print(differences)
Simple solution
import json
file1 = open("test.json","r") #opening first file with read permission
file2 = open("test1.json","r") #opening second file with read permission
#loading json files as dictionary objects
object1 = json.load(file1)
object2 = json.load(file2)
final_dict = {} #to store common elements
commonKeys = list(set(object1.keys()).intersection(object2.keys())) #finding common keys
for key in commonKeys:
if object1[key] == object2[key]: #if key,val matches in both json file
final_dict[key] = object1[key]
outputfile = open("output.json","w") #opening output file with write operation
json.dump(final_dict, outputfile) #saving final_disct to a json file
I'm trying to make a feature with a Discord bot where it counts how many commands have been run and it stores it on a JSON file named data.json. Here is the code:
import json
# Read 'data.json'
with open("data.json", "r") as d:
datar = json.load(d)
com = datar.get("commands-run")
# Functions
def command_increase():
commands_run = com + 1
dataw = open("data.json", "w")
dataw["commands-run"] = commands_run
And here is the JSON file:
{
"commands-run": 0
}
And this is the error I get when I run a command and it tries to increase the value in the JSON file:
TypeError: '_io.TextIOWrapper' object does not support item assignment
On top of that, it also completely wipes the JSON file. By that, I mean that it just clears everything. Even the brackets.
When you do a json.load, it loads your json data into a dict.
You can increase your command counter in this dict and re-write the dict back into your json file at the end of it.
import json
# Read 'data.json'
with open("data.json", "r") as d:
datar = json.load(d)
com = datar.get("commands-run")
# Functions
def command_increase():
commands_run = com + 1
datar["commands-run"] = commands_run
with open("data.json", "w") as dataw:
dataw.write(json.dumps(datar, indent=4))
command_increase()
I need to get data from this API https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434 (sample node)
This is my code (python):
import requests
f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')
print f.text
I want to save only protocol, responseTime and reputation in three subsequent lines of the txt file. It's supposed to look something like this::
protocol: 1.2.0
responseTime: 8157.912472694088
reputation: 1377
Unfortunately, I'm stuck at this point and I can not process this data in any way
import requests
f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')
# Store content as json
answer = f.json()
# List of element you want to keep
items = ['protocol', 'responseTime', 'reputation']
# Display
for item in items:
print(item + ':' + str(answer[item]))
# If you want to save in a file
with open("Output.txt", "w") as text_file:
for item in items:
print(item + ':' + str(answer[item]), file=text_file)
Hope it helps! Cheers
You just need to transform to a JSON object to be able to access the keys
import requests
import simplejson as json
f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')
x = json.loads(f.text)
print 'protocol: {}'.format(x.get('protocol'))
print 'responseTime: {}'.format(x.get('responseTime'))
print 'reputation: {}'.format(x.get('reputation'))
This is a very unrefined way to do what you want that you could build off of. You'd need to sub in a path/filename for text.txt.
import requests
import json
f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')
t = json.loads(f.text)
with open('text.txt', 'a') as mfile:
mfile.write("protocol: {0}".format(str(t['protocol'])))
mfile.write("responseTime: {0}".format(str(t['responseTime'])))
mfile.write("reputation: {0}".format(str(t['reputation'])))
I require 2 things to be done.
First, take the request object and save the object attribute values
to a file as values of some known keys. This file needs to be editable
after saving, ie, a user can modify the values of the keys(So I used
json format). This is handled in function
save_auth_params_to_file().
Second, get the file contents in a such a format that I can retrieve
the values using the keys. This is handled in function
get_auth_params_from_file.
import json
import os
SUCCESS_AUTH_PARAM_FILE = '/auth/success_auth_params.json'
def save_auth_params_to_file(request):
auth_params = {}
if request is not None:
auth_params['token'] = request.token
auth_params['auth_url'] = request.auth_url
auth_params['server_cert'] = request.server_cert
auth_params['local_key'] = request.local_key
auth_params['local_cert'] = request.local_cert
auth_params['timeout'] = request.timeout_secs
with open(SUCCESS_AUTH_PARAM_FILE, 'w') as fout:
json.dump(auth_params, fout, indent=4)
def get_auth_params_from_file():
auth_params = {}
if os.path.exists(SUCCESS_AUTH_PARAM_FILE):
with open(SUCCESS_AUTH_PARAM_FILE, "r") as fin:
auth_params = json.load(fin)
return auth_params
Question:
Is there a more pythonic way to achieve the 2 things ?
Any potential issues in the code which I have overlooked?
Any error conditions I have to take care ?
There are some things to be noted, yes:
i) When your request is None for some reason, you are saving an empty JSON object to your file. Maybe you'll want to write to your file only if request is not None?
auth_params = {}
if request is not None:
auth_params['token'] = request.token
auth_params['auth_url'] = request.auth_url
auth_params['server_cert'] = request.server_cert
auth_params['local_key'] = request.local_key
auth_params['local_cert'] = request.local_cert
auth_params['timeout'] = request.timeout_secs
with open(SUCCESS_AUTH_PARAM_FILE, 'w') as fout:
json.dump(auth_params, fout, indent=4)
ii) Why not create the dict all at once?
auth_params = {
'token': request.token,
'auth_url': request.auth_url,
'server_cert': request.server_cert,
'local_key': request.local_key,
'local_cert': request.local_cert,
'timeout': request.timeout,
}
iii) Make sure this file is in a SAFE location with SAFE permissions. This is sensitive data, like anything related to authentication.
iv) You are overwriting your file everytime save_auth_params_to_file is called. Maybe you mean to append your JSON to the file instead of overwriting? If that's the case:
with open(SUCCESS_AUTH_PARAM_FILE, 'a') as fout: