Extract nested JSON values - python

I am trying to figure out on how to convert the following JSON object in to a dataframe
[
{
"id":"123",
"sources":[
{
"name":"ABC",
"first":"2020-02-26T03:19:23.247Z",
"last":"2020-02-26T03:19:23.247Z"
},
{
"name":"XYZ",
"first":"2020-02-26T03:19:23.247Z",
"last":"2020-02-26T03:19:23.247Z"
}
]
}
]
The dataframe should appear like this.
id ABC.first ABC.last XYZ.first XYZ.last
123 2020-02-26.. 2020-02-26.. 2020-02-26.. 2020-02-26T03:19:23.247Z
Thanks in advance for your help

Python includes the useful json module. You can find some documentation for it here.
To use it , you'll need to import it into your Python script using the import json command.
From there, you can use the json.loads() method and pass it a some JSON. The loads() method returns a Python dictionary which then lets you access the properties of your JSON object by name. Some example code might look like:
import json
# some JSON:
personJSON = '{"name":"John", "age":30, "city":"New York", "address": {"street": "Fake Street", "streetNumber": "123"}}'
# parse personJSON:
personDict = json.loads(personJSON)
# the result is a Python dictionary:
print(personDict["name"]) # Will print "John"
print(personDict["address"]["street"]) # Will print "Fake Street"
Good luck!

Related

Python JSON formatting

So I have a problem with JSON. The data that I receive from an API looks like this.
{
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04d"
}
]
}
I can't read the data in the weather cell tough because it's wrapped between those '[ ]'. But if I try to create a JSON file but remove the "[ ]" and try to read it, it works. What can I do? Please help!
If I do the following it works just fine:
import json
data = '''{
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04d"
}
]
}'''
dict_data = json.loads(data)
print(dict_data.get("weather")[0].get("main"))
>>> "Clouds"
It works as expected. Because it is a list, you have to target the first item, here another dict, which holds the information you want.

New Learner - Need Help in prasing nested json in python

I am trying to get a particular value from nested JSON
JSON is as below
{
"ListOn": true,
"currentList": "Counter_Master",
"deployedList": [
{
"name": "Master",
"skipForSchedule": false,
"Type": "regular"
},
{
"plType": "regular",
"skipForSchedule": false,
"name": "Name_test"
}
],
"uptime": 1216.819
}
This is what I did till now
import json
with open('config.json') as f:
#print("file is open")
data = json.load(f)
curr_list = data['deployedPlaylists'] [1] . values()
curr_list = curr_playlist[1]
print("we are finding the name of curr_list[1]", curr_list)
I am not able to pickup 2nd name which is "Name_test" in deployedList[1].name
from shell, I can do with below command, want to do in python, but as I am new to python could not find the exact way.
sudo jq -r '.deployedlists[1].name' _config.json
Requesting for help.
Thanks
I think you just have typos in your code
Just fix data['deployedPlaylists'] [1] . values() to be data['deployedList'] [1]['name'] which will be storing the value Name_test as required.
just take in mind that json.read() returns the value of the json file in python dictionary so to manipulate the values of the json file read more about python dictionaries how they are stored and how to access their values.

JSON Parse an element inside an element in Python

I have a JSON text grabbed from an API of a website:
{"result":"true","product":{"made":{"Taiwan":"Taipei","HongKong":"KongStore","Area":"Asia"}}}
I want to capture "Taiwan" and "Taipei" but always fail.
Here is my code:
import json
weather = urllib2.urlopen('url')
wjson = weather.read()
wjdata = json.loads(wjson)
print wjdata['product']['made'][0]['Taiwan']
I always get the following error:
Keyword 0 error
Whats the correct way to parse that json?
You are indexing an array where there are none.
The JSON is the following:
{
"result":"true",
"product": {
"made": {
"Taiwan":"Taipei",
"HongKong":"KongStore",
"Area":"Asia"
}
}
}
And the above contains no arrays.
You are assuming the JSON structure to be something like this:
{
"result":"true",
"product": {
"made": [
{"Taiwan":"Taipei"},
{"HongKong":"KongStore"},
{"Area":"Asia"}
]
}
}
From a brief look at the doc pages for the json package, I found this conversion table: Conversion table using json.loads
It tells us that a JSON object translates to a dict. And a dict has a method called keys, which returns a list of the keys.
I suggest you try something like this:
#... omitted code
objectKeys = wjdata['product']['made'].keys()
# You should now have a list of the keys stored in objectKeys.
for key in objectKeys:
print key
if key == 'Taiwan':
print 'Eureka'
I haven't tested the above code, but I think you get the gist here :)
wjdata['product']['made']['Taiwan'] works

Grab values from JSON objects in Python and assign them to a variable

I would like to grab some values within a JSON object using Python and assign them to variables for further use in the html frontend.
I tried it using the w3c resource and by googling but I can't get any successful print:
import requests
import json
response = requests.get("https://www.api-football.com/demo/api/v2/teams/team/33")
team_data = response.json()
team_name = team_data.teams.name
print(team_name)
This is the JSON Object i get from the external API:
{
"api": {
"results": 1,
"teams": [
{
"team_id": 33,
"name": "Manchester United",
"code": "MUN",
"logo": "https://media.api-football.com/teams/33.png",
"country": "England",
"founded": 1878,
"venue_name": "Old Trafford",
"venue_surface": "grass",
"venue_address": "Sir Matt Busby Way",
"venue_city": "Manchester",
"venue_capacity": 76212
}
]
}
}
The debug console tells me AttributeError: 'dict' object has no attribute 'teams'
JSONs (JavaScript Object Notation) are overall JavaScript objects used for storing serialized data. The python interpreter has an specific module import jsonthat allows to charge JSON files to Python dicts in the RAM of your machine.
In your situation:
with open('team_data.json', "r") as fobj:
dataset = json.load(fobj)
print('Teams data: ', dataset['api']['teams'])
From here you can work with it as a normal dict.

Confused by Python returning JSON as string instead of literal

I've done some coding in RoR, and in Rails, when I return a JSON object via an API call, it returns as
{ "id" : "1", "name" : "Dan" }.
However in Python (with Flask and Flask-SQLAlchemy), when I return a JSON object via json.dumps or jsonpickle.encode it is returned as
"{ \"id\" : \"1\", \"name\": \"Dan\" }" which seems very unwieldily as it can't easily be parsed on the other end (by an iOS app in this case - Obj-C).
What am I missing here, and what should I do to return it as a JSON literal, rather than a JSON string?
This is what my code looks like:
people = models.UserRelationships.query.filter_by(user_id=user_id, active=ACTIVE_RECORD)
friends = people.filter_by(friends=YES)
json_object = jsonpickle.encode(friends.first().as_dict(), unpicklable=False, keys=True)
print(json_object) # this prints here, i.e. { "id" : "1", "name" : "Dan" }
return json_object # this returns "{ \"id\" : \"1\", \"name\": \"Dan\" }" to the browser
What is missing in your understanding here is that when you use the JSON modules in Python, you're not working with a JSON object. JSON is by definition just a string that matches a certain standard.
Lets say you have the string:
friends = '{"name": "Fred", "id": 1}'
If you want to work with this data in python, you will want to load it into a python object:
import json
friends_obj = json.loads(friends)
At this point friends_obj is a python dictionary.
If you want to convert it (or any other python dictionary or list) then this is where json.dumps comes in handy:
friends_str = json.dumps(friends_obj)
print friends_str
'{"name": "Fred", "id": 1}'
However if we attempt to "dump" the original friends string you'll see you get a different result:
dumped_str = json.dumps(friends)
print dumped_str
'"{\\"name\\": \\"Fred\\", \\"id\\": 1}"'
This is because you're basically attempting to encode an ordinary string as JSON and it is escaping the characters. I hope this helps make sense of things!
Cheers
Looks like you are using Django here, in which case do something like
from django.utils import simplejson as json
...
return HttpResponse(json.dumps(friends.first().as_dict()))
This is almost always a sign that you're double-encoding your data somewhere. For example:
>>> obj = { "id" : "1", "name" : "Dan" }
>>> j = json.dumps(obj)
>>> jj = json.dumps(j)
>>> print(obj)
{'id': '1', 'name': 'Dan'}
>>> print(j)
{"id": "1", "name": "Dan"}
>>> print(jj)
"{\"id\": \"1\", \"name\": \"Dan\"}"
Here, jj is a perfectly valid JSON string representation—but it's not a representation of obj, it's a representation of the string j, which is useless.
Normally you don't do this directly; instead, either you started with a JSON string rather than an object in the first place (e.g., you got it from a client request or from a text file), or you called some function in a library like requests or jsonpickle that implicitly calls json.dumps with an already-encoded string. But either way, it's the same problem, with the same solution: Just don't double-encode.
You should be using flask.jsonify, which will not only encode correctly, but also set the content-type headers accordingly.
people = models.UserRelationships.query.filter_by(user_id=user_id, active=ACTIVE_RECORD)
friends = people.filter_by(friends=YES)
return jsonify(friends.first().as_dict())

Categories