Read specific JSON object from response in Python [duplicate] - python

This question already has answers here:
Accessing elements of Python dictionary by index
(11 answers)
Closed 6 years ago.
When you have a JSON response that contains multiple JSON objects, how do you pull out a specific object within the JSON using Python?
For example, with the following JSON response, I have three objects in it.
{
"_links": {
"base": "REDACTED",
"context": "",
"self": "REDACTED"
},
"limit": 20,
"results": [
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "",
"container": "",
"descendants": "",
"extensions": "",
"history": "/rest/api/content/198121503/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/ReleaseNotes",
"version": ""
},
"_links": {
"self": "REDACTED",
"tinyui": "/x/HxjPCw",
"webui": "UNIQUE_URL_HERE"
},
"id": "198121503",
"status": "current",
"title": "Unique Title of Content",
"type": "page"
},
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "",
"container": "",
"descendants": "",
"extensions": "",
"history": "/rest/api/content/197195923/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/ReleaseNotes",
"version": ""
},
"_links": {
"self": "REDACTED",
"tinyui": "/x/k-jACw",
"webui": "UNIQUE_URL_HERE"
},
"id": "197195923",
"status": "current",
"title": "Unique Title of Content",
"type": "page"
},
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "",
"container": "",
"descendants": "",
"extensions": "",
"history": "/rest/api/content/198121203/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/ReleaseNotes",
"version": ""
},
"_links": {
"self": "REDACTED",
"tinyui": "/x/8xbPCw",
"webui": "UNIQUE_URL_HERE"
},
"id": "198121203",
"status": "current",
"title": "Unique Title of Content",
"type": "page"
}
],
"size": 3,
"start": 0
}
How can I retrieve the ID and TITLE for a specific object in the response?
I read in other threads that when you use json.loads(your_json), it becomes a dictionary. If that's the case, how do I pull this data if it's stored as a dictionary?
Update
Let me clarify, as maybe I'm not seeing or explaining this clearly.
Is the only option to cycle through everything? There's not an option to say get me the 2nd JSON object and return the ID and Title? If that's the case, why shouldn't I create a custom object, store the items I want from each JSON object into those within an array, then I can access each object within the array?

After you transform your response to json, you can just use key attributes.
for result in data['results']:
print("id: {}, title: {}".format(result['id'], result['title']))
As you mentioned, you can use json.load to transform string to dictionary. But if you're using requests library, just use response.json to get data in required format.

Use bracket notation to access the keys after loading the string into a json object. Loop through the results key until you find the object you want, just like this:
j = json.loads(your_json)
for r in j["results"]:
if r["title"] == "Something":
print(r["id"])
print(r["title"])

Related

How to get documents' objects length in MongoDB using PyMongo?

I have hundreds of documents following the format below. Basically, each doc contains objects with the types "BehavioralData", "Notes", etc.
{
"_id": "123",
"Notes": {
"1222": "Something is here"
},
"BehavioralData": {
"Folder1": {
"Sex": "Male",
"Age": "22",
"Date": "",
"ResearchGroup": "",
"Institution": "University of Manitoba"
},
"MoCA": {
"Visual-Executive": "",
"Naming": "NameHere",
"Attention": "",
"Language": "",
"Abstraction": "",
"Delayed Recall": "",
"Orientation": "",
"Education": "",
"Total": ""
}
}
}
Is there a way to query the collection using PyMongo in Python to get the following result:
{
"NotesLength": 1,
"BehavioralLength": 2
}
What I did before, is upload documents one by one to my python script, and then measured the lengths of the dictionaries inside the dictionary. This takes a bit long as my python program queries the whole collection. Is there a way to do it faster than I do now?

How to modify a string inside a JSON object and convert it into another JSON object and thus add a key value with Python?

I would like to find a way to convert a string inside my JSON object into another JSON object (nested) with the use of Python and thus add a key to the values inside my string that are separated by the use of a ";".
My current JSON object:
{
"reference": "#############",
"messageContext": "",
"from": {
"number": "+#############",
"name": ""
},
"to": {
"number": "#############"
},
"message": {
"text": "12302;8;6245;d517666e-41ca-459a-9b35-c49386df537b;2;2;50.8447;-4.3614;2021-04-28T22:24:12.204Z;rec123;PRD",
"media": {
"mediaUri": "",
"contentType": "",
"title": ""
},
"custom": {}
},
"groupings": [
"",
"",
""
],
"time": "2021-05-02 14:03:22",
"timeUtc": "2021-05-02T12:03:22",
"channel": "Sms"
}
The format I would try to have is something like this, where I would change the text to an object while adding key values.
Result, which I try to obtain (Inside of text:" "):
{
"reference": ""#############",",
"messageContext": "",
"from": {
"number": "+"#############",",
"name": ""
},
"to": {
"number": ""#############","
},
"message": {
"text": {
"APSI": "12302",
"idVhl": 8,
"idDrv": 6245,
"GUID": "d517666e-41ca-459a-9b35-c49386df537b",
"idLne": 2,
"idSvc": 2,
"Lat": 50.8447,
"Lon": -4.3614,
"Timestamp": "2021-04-28T22:24:12.204Z",
"Rec": "rec123",
"Env": "PRD"
},
"media": {
"mediaUri": "",
"contentType": "",
"title": ""
},
"custom": {}
},
"groupings": [
"",
"",
""
],
"time": "2021-05-02 14:03:22",
"timeUtc": "2021-05-02T12:03:22",
"channel": "Sms"
}
Given your JSON object as input this function will return your expected output.
def transform(input):
text = input['message']['text']
text_list = text.split(';')
text_dict = {
'APSI': text_list[0],
'idVhl': int(text_list[1]),
'idDrv': int(text_list[2]),
'GUID': text_list[3],
'idLne': int(text_list[4]),
'idSvc': int(text_list[5]),
'Lat': float(text_list[6]),
'Lon': float(text_list[7]),
'Timestamp': text_list[8],
'Rec': text_list[9],
'Env': text_list[10]
}
input['message']['text'] = text_dict
return input
This is done by splitting your text on the ; character which returns a list of all the values you want. You can then parse the list into a dictionary and then assign that dictionary to your original text value.

Using Response of Confluence Rest API

I am performing bulk operation of creating Space and Pages in our Confluence Cloud instance. I am not particularly a programmer. Need some assistance in using the response we are getting after running the APIs using Python. The output is in Json format. If this is the output can you please let me know how can I access the title and ID -
{
"content": {
"id": "398852913",
"type": "page",
"status": "current",
"title": "Test Project Name 1 SOW",
"childTypes": {},
"macroRenderedOutput": {},
"restrictions": {},
"_expandable": {
"container": "",
"metadata": "",
"extensions": "",
"operations": "",
"children": "",
"history": "/rest/api/content/398852913/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "",
"space": "/rest/api/space/TestSpace1",
},
"_links": {
"webui": "/spaces/TestSpace1/pages/398852913/Test+Project+Name+1++SOW",
"self": "https://enerzinx.atlassian.net/wiki/rest/api/content/398852913",
"tinyui": "/x/MQPGFw",
},
},
"title": "Test Project Name 1 SOW",
"excerpt": "file-list",
"url": "/spaces/TestSpace1/pages/398852913/Test+Project+Name+1++SOW",
"resultGlobalContainer": {
"title": "TestSpace1",
"displayUrl": "/spaces/TestSpace1",
},
"breadcrumbs": [],
"entityType": "content",
"iconCssClass": "aui-iconfont-page-default",
"lastModified": "2020-06-24T06:17:32.333Z",
"friendlyLastModified": "Jun 24, 2020",
"score": 0.59390986,
}
If your response is a string in JSON format first you will need to parse the JSON into a dictionary. For this you can use the json module, which is part of the set of standard modules. After parsing the JSON you can then access the keys using a dictionary lookup.
>>> import json
>>>
>>> json_string = '{"foo": "bar"}'
>>> json_dict = json.loads(json_string)
>>> json_dict["foo"]
'bar'
It looks like your response JSON will yield a nested dictionary, so the path for the title will look something like json_dict["foo"]["bar"].

check if json element or object exists or not and proceed

Hi im am trying to parse json data and gets this error every time the element
if ['fields']['assignee'] in each:
TypeError: list indices must be integers or slices, not str
>>>
My json is this
{
"expand": "schema,names",
"startAt": 1,
"maxResults": 50,
"total": 7363,
"issues": [
{
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id": "591838",
"self": "https://jira.mynet.com/rest/api/2/issue/591838",
"key": "TEST-8564",
"fields": {
"summary": "delete tables 31-03-2020 ",
"customfield_10006": 2.0,
"created": "2020-02-27T10:29:12.000+0100",
"description": "A LOT OF TEXT",
"assignee": null,
"labels": [
"DATA",
"Refined"
],
"status": {
"self": "https://jira.mynet.com/rest/api/2/status/10000",
"description": "",
"iconUrl": "https://jira.mynet.com/",
"name": "To Do",
"id": "10000",
"statusCategory": {
"self": "https://jira.mynet.com/rest/api/2/statuscategory/2",
"id": 2,
"key": "new",
"colorName": "blue-gray",
"name": "To Do"
}
}
}
}
]
}
The element in ['fields']['assignee'] is NULL in this example
sometimes it is like this
"assignee": : {
"self": "https://mynet.com/rest/api/2/user?username=xxxxxx",
"name": "sij",
"key": "x",
"emailAddress": xx#mynet.com",
"avatarUrls": {
"48x48": "https://mynet.com/secure/useravatar?ownerId=bdysdh&avatarId=16743",
"24x24": "https://mynet.com/secure/useravatar?size=small&ownerId=bdysdh&avatarId=16743",
"16x16": "https://mynet.com/secure/useravatar?size=xsmall&ownerId=bdysdh&avatarId=16743",
"32x32": "https://mynet.com/secure/useravatar?size=medium&ownerId=bdysdh&avatarId=16743"
},
"displayName": "Bruce Springsteen",
"active": true,
"timeZone": "Arctic/Longyearbyen"
},
I am trying to check of assignee is null and if so print null
my code looks like this
with open('C:\\TEMP\\testdata.json') as json_file:
data = json.load(json_file)
for each in data['issues']:
if ['fields']['assignee'] in each:
print (['fields']['assignee']['name'])
else:
print ('null')
I have tried to put in [0] between ['fields']['assignee']['name'] but nothing seems to help.
Try with
if 'fields' in each and 'assignee' in each['fields']:
Note that you need the name of the key, not surrounded by square brackets.
Perhaps better:
for each in data['issues']:
print(each.get('fields', {}).get('assignee', {}).get('name', 'null'))
and if you can't guarantee that 'issues' exists in data either:
for each in data.get('issues', []):
<as before>
data.get('issues', []) returns an empty list if data['issuess'] doesn't exist.

How to Fetch the value of any item from the JSON output in Python?

I have a function in python which is fetching the data in JSON format and I need to get the value of one item and store it in variable so I could use it another function
import requests
import json
import sys
def printResponse(r):
print '{} {}\n'.format(json.dumps(r.json(),
sort_keys=True,
indent=4,
separators=(',', ': ')), r)
r = requests.get('https://wiki.tourist.com/rest/api/content',
params={'title' : 'Release Notes for 1.1n1'},
auth=('ABCDE', '*******'))
printResponse(r)
getPageid = json.loads(r)
value = int(getPageid['results']['id'])
I am trying to get the value of id(160925) item in variable "value" so I could use it another function
Below is the JSON OUTPUT
{
"_links": {
"base": "https://wiki.tourist.com",
"context": "",
"self": "https://wiki.tourist.com/rest/api/content?title=Notes+for+1.1u1"
},
"limit": 25,
"results": [
{
"_expandable": {
"ancestors": "",
"body": "",
"children": "/rest/api/content/160925/child",
"container": "",
"descendants": "/rest/api/content/160925/descendant",
"history": "/rest/api/content/160925/history",
"metadata": "",
"operations": "",
"space": "/rest/api/space/Next",
"version": ""
},
"_links": {
"self": "https://wiki.tourist.com/rest/api/content/160925412",
"tinyui": "/x/5IaXCQ",
"webui": "/display/Next/Notes+for+1.1u1"
},
"extensions": {
"position": "none"
},
"id": "160925",
"status": "current",
"title": "Notes for 1.1u1",
"type": "page"
}
],
"size": 1,
"start": 0
} <Response [200]>
It looks like the "results" key in the JSON response corresponds to a list, so you'll want to index into that list to get a dict.
E.g. getPageid['results'][0]['id'] should return you the string value of the "id" key for the first object in the "results" list

Categories