I want to parse a Json File where all Json Arrays have the same Name just as the following:
[
{
"envelope": {
"source":"user1",
"data":{
"message": "Hello World 0"
}
}
},
{
"envelope": {
"source":"user1",
"data":{
"message": "Hello World 1"
}
}
},
...
]
And this is my code so far:
def check_messages():
data = requests.get(urlWhereIGetJson)
for d in data:
message = d['envelope']['data']['message']
print(message)
My code is only giving me back the first entry. ("Hello World 0") but I need every message.
I hope somebody can teach me and show how to parse the json correctly.
I am not able to edit the JSON File.
I'm sorry for my english
Here is what you need
response = requests.get(urlWhereIGetJson)
if response.status_code == 200:
data = json.loads(response.text)
for record in data:
print(record['envelope']['data']['message'])
For more information https://www.w3schools.com/python/ref_requests_response.asp
Saeed already covered how to do this, but if you would like it in a nice Pandas DataFrame, you could do something like this:
data = json.load('file.json')
series = pd.Series(data) # Since it's an array, I'll turn that into a Series first
df = pd.json_normalize(series)
That DataFrame would automatically unnest inner json objects and represent them with column names like envelope.source and envelope.data.message.
Edit: Also if you did want this to read the json from requests, just use json.loads(data.json())
Related
I am creating a python project who are working with an api, the api return an json like this:
"1": "2018-10-13T08:28:38.809469028Z",
"result": [
{
"id":3027531,
"created_at":"2018-10-13T08:20:38.809469028Z",
"date":"2018-10-13T08:19:38Z",
"text":"banana",
}
],
I can get 1, but i can't get text in result, can someone help me?
I tried:
response.json()['result']['text']
response.json()['result'].text
response.json().result[0].text
Try this
response_json = response.json()
response_json["result"][0]["text"]
The value for result is a list of dictionaries. We take the first item in that list, and then we ask for the value of text.
Be aware that this assumes that response_json["result"] has at least one item. If it is empty, you will get an IndexError. You should probably check the length of response_json["result"] before using it. Here is an example of how this could be done:
response_json = response.json()
if response_json["result"]:
response_json["result"][0]["text"]
else:
print("result is empty")
d = {"1": "2018-10-13T08:28:38.809469028Z",
"result": [
{
"id":3027531,
"created_at":"2018-10-13T08:20:38.809469028Z",
"date":"2018-10-13T08:19:38Z",
"text":"banana",
}
]}
d['result'][0]['id'] # 3027531
d['result'][0]['text'] # banana
I've been experimenting with python for a while and just ran into an issue I can't find an answer to. Just started building a small 'banking' console app and it's "database" is in JSON file. Now I added a command called 'transfer' which should transfer 'funds' from one user_id to another.
JSON file looks like this:
{
"users": [
{
"user_id": "u111111111",
"pin": "2222",
"balance": "50800"
},
{
"user_id": "u123456789",
"pin": "1234",
"balance": "0"
}
]
}
Last thing I tried was something like this and got completely stuck.
user_database = 'accounts.json'
ujf = json.loads(open(user_database, 'r+').read())
for element in ujf['users']:
if (element['user_id'] == str(user_id_one)):
element['balance'] = str(user_one_new_balance)
else:
pass
How do I update the 'balance' value for user_id u111111111 and for user_id u123456789 and save the file, without creating a new file?
First import JSON module:
import json
Then load the JSON file as dictionary:
d = json.load(r'PATH')
Then inside the loop you can assign user['balance'] to whatever you like, e.g. 8 here:
for user in d['users']:
if user['user_id'] in ['u111111111','u123456789']:
user['balance'] = 8
Then dump back:
json.dump(d,(open(r'PATH','w')))
I'm kinda new JSON and python and i wish to use the keys and values of JSON to compare it.
I'm getting the JSON from a webpage using requests lib.
Recently, I've done this:
import requests;
URL = 'https://.../update.php';
PARAMS = { 'platform':'0', 'authcode':'code', 'app':'60' };
request = requests.get( url=URL, params=PARAMS );
data = request.json( );
I used this loop to get the keys and values from that json:
for key, value in data.items( ):
print( key, value );
it return JSON part like this:
rescode 0
desc success
config {
"app":"14",
"os":"2",
"version":"6458",
"lang":"zh-CN",
"minimum":"5",
"versionName":"3.16.0-6458",
"md5":"",
"explain":"",
"DiffUpddate":[ ]
}
But in Firefox using pretty print i get different result look like this:
{
"rescode": 0,
"desc": "success",
"config": "{
\n\t\"app\":\"14\",
\n\t\"os\":\"2\",
\n\t\"version\":\"6458\",
\n\t\"lang\":\"zh-CN\",
\n\t\"minimum\":\"5\",
\n\t\"versionName\":\"3.16.0-6458\",
\n\t\"md5\":\"\",
\n\t\"explain\":\"\",
\n\t\"DiffUpddate\":[\n\t\t\n\t]\n
}"
}
What I'm planing to do is:
if data['config']['version'] == '6458':
print('TRUE!');
But everytime i get this error:
TypeError: string indices must be integers
You need to parse the config
json.loads(data['config'])['version']
Or edit the PHP to return an associative array rather than a string for the config object
I'm trying to access certain fields of information in JSON dict. My code is set up as the following:
Views.py
def viewIssues(request):
r = requests.get(bucket_url)
issue_payload = r.json()
issue = json.loads(str(issue_payload))
context = {
"issue_title": issue['issues']['title'],
"issue_content": issue['issues']['content'],
"title": "View Issues",
}
return render(request, "view_issues.html", context)
str(issue_payload) gives me this:
{
'search':None,
'count':1,
'filter':{
},
'issues':[
{
'priority':'major',
'comment_count':0,
'utc_created_on':'2016-11-12 01:48:16+00:00',
'utc_last_updated':'2016-11-12 01:48:16+00:00',
'status':'new',
'title':'example issue',
'reported_by':{
'is_staff':False,
'display_name':'display name',
'is_team':False,
'resource_uri':'/1.0/users/username',
'avatar':'https://bitbucket.org/account/username/avatar/32/?ts=1479493904',
'first_name':'firstname',
'username':'username',
'last_name':'lastname'
},
'is_spam':False,
'content':'blah blah',
'metadata':{
'milestone':None,
'component':None,
'version':None,
'kind':'bug'
},
'local_id':1,
'created_on':'2016-11-12T02:48:16.052',
'resource_uri':'/1.0/repositories/username/supportal2016test/issues/1',
'follower_count':1
}
]
}
However when I try to use the json.loads and indices ['issues']['title'] and ['issues']['title'] I get an error:
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I'm wondering if it's because the converted payload has quotations on each field (i.e. 'issues'). Any help would be much appreciated.
The .json() call already parses the JSON result and returns a Python structure in this case a dictionary. Then your call
issue = json.loads(str(issue_payload))
forces the dictionary into a string and tries to parse it again. But the dictionary string representation contains ' around strings and not " as required in JSON.
To cut the long story short: issue_payload is what you want already.
I've got a json file that I've pulled from a web service and am trying to parse it. I see that this question has been asked a whole bunch, and I've read whatever I could find, but the json data in each example appears to be very simplistic in nature. Likewise, the json example data in the python docs is very simple and does not reflect what I'm trying to work with. Here is what the json looks like:
{"RecordResponse": {
"Id": blah
"Status": {
"state": "complete",
"datetime": "2016-01-01 01:00"
},
"Results": {
"resultNumber": "500",
"Summary": [
{
"Type": "blah",
"Size": "10000000000",
"OtherStuff": {
"valueOne": "first",
"valueTwo": "second"
},
"fieldIWant": "value i want is here"
The code block in question is:
jsonFile = r'C:\Temp\results.json'
with open(jsonFile, 'w') as dataFile:
json_obj = json.load(dataFile)
for i in json_obj["Summary"]:
print(i["fieldIWant"])
Not only am I not getting into the field I want, but I'm also getting a key error on trying to suss out "Summary".
I don't know how the indices work within the array; once I even get into the "Summary" field, do I have to issue an index manually to return the value from the field I need?
The example you posted is not valid JSON (no commas after object fields), so it's hard to dig in much. If it's straight from the web service, something's messed up. If you did fix it with proper commas, the "Summary" key is within the "Results" object, so you'd need to change your loop to
with open(jsonFile, 'w') as dataFile:
json_obj = json.load(dataFile)
for i in json_obj["Results"]["Summary"]:
print(i["fieldIWant"])
If you don't know the structure at all, you could look through the resulting object recursively:
def findfieldsiwant(obj, keyname="Summary", fieldname="fieldIWant"):
try:
for key,val in obj.items():
if key == keyname:
return [ d[fieldname] for d in val ]
else:
sub = findfieldsiwant(val)
if sub:
return sub
except AttributeError: #obj is not a dict
pass
#keyname not found
return None