Json into dataframe pandas - python

I have below json which I've got from some url:
{
"abc":
{
"123":[45600,null,3567],
"378":[78689,2345,5678],
"343":[23456,null,null]
}
}
it is stored in json_obj object:
json_obj = response.json()
I need to convert this json into dataframe, My code should be something like this :
df = pd.read_json(response,orient='columns')
so, that result should be :
abc
123 [45600,null,3567]
378 [78689,2345,5678]
343 [23456,null,null]
But with above code I get error :
date_unit).parse()
self._parse_no_numpy()
loads(json, precise_float=self.precise_float), dtype=None)
TypeError: Expected String or Unicode
If I replace response with url in the above code. it will work fine. But, I need to pass json_object instead of url.
Please give suggestions.

df = pd.read_json(response,orient='columns')
read_json() takes in JSON data. "response", i believe stores the API response for some request you're making.
response.json() would give you the python dictionary of the body of the response.
You need to convert that to JSON.
Try this:
import json
df = pd.read_json(json.dumps(response.json()),orient='columns')

Related

How to parse mulitple Json Arrays with the same name in Python

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())

I've been trying to make a json file out of a list that has dictionaries

For example
I have tried
import json
data = [{"ModelCode":"VH017","MakeCode":"VM020","VehicleTypeCode":"VC00000052","Year":2017,"IsActive":true,"RegistrationNumber":"KCC 254 ZY","IsApproved":true,"ApprovedBy":null,"Color":"BLUE","Id":"8c5062da-727b-40d5-b763-408cafdc53d8","_id":"3ce92939-4df7-4b9e-af48-647e218736da"},{"ModelCode":"VH024","MakeCode":"VM026","VehicleTypeCode":"VC00000053","Year":2008,"IsActive":false,"RegistrationNumber":"kkk 333k","IsApproved":false,"ApprovedBy":null,"Color":"blue","Id":"8c5062da-727b-40d5-b763-408cafdc53d8"}]
data_from_api = data.strip('][').split(',')
json.loads(data_from_api)
print(data_from_api)
I get a "NameError: name 'true' is not defined"
json.loads(str) is used to import data from a json string. If you want to create a json string from python data to save in a file as your header suggest then use json.dumps instead.
And as comments suggest. In python null is spelled None, false is False and true is True
import json
data = [
{
"ModelCode":"VH017",
"IsActive":True,
"ApprovedBy":None
},{
"ModelCode":"VH024",
"IsActive":False,
"ApprovedBy":None
}
]
json_str = json.dumps(data)
print(json_str)

python : get specific value from json string

I Need to Exact Value from Json response
how can I get a specific json value python ???
this is my Json string
I need to extract Id value
"window.__store__ ="{
"listingReducer":{
"selectedListing":{
"id":2234588,
"has_video":0,
"refresh":1625551240,
"category":6,
"ketchen":1,
"lift":1,
"livings":1,
"maid":null,
"meter_price":null,
"playground":null,
"location":{
"lat":26.378031,
"lng":50.124866
},
"views":6075,
},3
}
}
this my python code :
import json
from selenium import webdriver
jsonScript =driver.find_element_by_xpath("/html/body/script[6]")
jsonText = jsonScript.get_attribute('innerHTML')
.
.
.
json_str = json.dumps(jsonText)
resp = json.loads(json_str)
#print (resp)
print(resp[0]['listingReducer']['selectedListing']['id'])
.
.
.
And this is the Error
TypeError: string indices must be integers
Your string is not in JSON format.
I have managed to convert it into JSON by removing irrelevant content (that doesn't follow JSON encoding).
This code will get you the ID from the JSON string.
import json
s = '''
{
"listingReducer":{
"selectedListing":{
"id":2234588,
"has_video":0,
"refresh":1625551240,
"category":6,
"ketchen":1,
"lift":1,
"livings":1,
"maid":null,
"meter_price":null,
"playground":null,
"location":{
"lat":26.378031,
"lng":50.124866
},
"views":6075
}
}
}
'''
d = json.loads(s)
print(f"ID: {d['listingReducer']['selectedListing']['id']}")
Output:
ID: 2234588

Convert String to Json type in python with socket programming

data = data.decode('utf-8')
print(data) # b'1' : {"reciever": "1", "sender:": 1, "seq_num": 7, "data": "2"}
data = data[7:]
print(data) # {"reciever": "1", "sender:": 1, "seq_num": 7, "data": "2"}
data = json.dumps(data)
data = json.loads(data)
print(type(data)) #<class 'str'>
Here is my code. I try to send data (string + json) with socket programming.
It sends data well, but I want to use json when receiving messages.
So I sliced the json part, and dumps and loads into json. (I imported json)
But Its type is still in str.
In my sending function,
result = json.dumps({'reciever' : rec, 'sender:': sender, 'seq_num' : sequence_number, 'data': data})
final_result = json.loads(result)
print(type(final_result)). #dict type
It seems works well here.
Please help !
Thank you
remove data = json.dumps(data).
json.dumps saves json object to str. but data is already str in your code.

How to save API response to csv with Python

I'm getting facedetection data from an API in this form:
{"id":1,"ageMin":0,"ageMax":100,"faceConfidence":66.72220611572266,"emotion":"ANGRY","emotionConfidence":50.0'
b'2540969848633,"eyeglasses":false,"eyeglassesConfidence":50.38102722167969,"eyesOpen":true,"eyesOpenConfidence":50.20328140258789'
b',"gender":"Male","genderConfidence":50.462989807128906,"smile":false,"smileConfidence":50.15522384643555,"sunglasses":false,"sun'
b'glassesConfidence":50.446510314941406}]'
I'd like to save this to a csv-file like this:
id ageMin ageMax faceConfidence
1 0 100 66
... and so on.
I tried to do it this way:
response = requests.get(url, headers=headers)
with open('detections.csv', 'w') as f:
writer = csv.writer(f)
for item in response:
writer.writerow(str(item))
That puts every char in its own cell. I've also tried to use item.id, but that gives an error: AttributeError: 'bytes' object has no attribute 'id'.
Could someone point me to the right direction?
Maybe an overkill for a small task, but you can do the following:
convert JSON response (do not forget to check exceptions, etc.) to python dictionary
dic = response.json()
Create a dataframe, for example using pandas:
df = pandas.DataFrame(dic)
Save to csv omitting index:
df.to_csv('detections.csv', index=False, sep="\t")
You can do this relatively easily with the pandas and json libraries.
import pandas as pd
import json
response = """{
"id": 1,
"ageMin": 0,
"ageMax": 100,
"faceConfidence": 66.72220611572266,
"emotion": "ANGRY",
"emotionConfidence": 50.0,
"eyeglasses": false,
"eyeglassesConfidence": 50.38102722167969,
"eyesOpen": true,
"eyesOpenConfidence": 50.20328140258789,
"gender": "Male",
"genderConfidence": 50.462989807128906,
"smile": false,
"smileConfidence": 50.15522384643555,
"sunglasses": false,
"glassesConfidence":50.446510314941406
}"""
file = json.loads(doc)
json = pd.DataFrame({"data": file})
json.to_csv("response.csv")
This is the response formatted to csv.
,data
ageMax,100
ageMin,0
emotion,ANGRY
emotionConfidence,50.0
eyeglasses,False
eyeglassesConfidence,50.38102722167969
eyesOpen,True
eyesOpenConfidence,50.20328140258789
faceConfidence,66.72220611572266
gender,Male
genderConfidence,50.462989807128906
glassesConfidence,50.446510314941406
id,1
smile,False
smileConfidence,50.15522384643555
sunglasses,False

Categories