If I have Json data that looks like this:
import json
rawData= {
"Date": "2020-08-16T13:37:22.501743", "MeterReading": 199
}
jsonData = json.dumps(rawData)
print(jsonData)
How could I verify with a boolean statement jsonData contains a legitimate time stamp (that pandas would understand) and the MeterReading would either be a float or integer value?
Is converting json object to list marching down the correct path?
l = list(rawData.values())
I can print these values individually but I think they are still strings...?
print(l[0], l[1])
Any tips greatly appreciated..
Related
I have a JSON object that looks as such, stored in a variabled called results:
{
"users":[
{
"pk":54297756964,
"username":"zach_nga_test",
"full_name":"",
"is_private":false,
"profile_pic_url":"https://instagram.fcxl1-1.fna.fbcdn.net/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?efg=eyJybWQiOiJpZ19hbmRyb2lkX21vYmlsZV9uZXR3b3JrX3N0YWNrX2JhY2t0ZXN0X3YyNDI6Y29udHJvbCJ9&_nc_ht=instagram.fcxl1-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=bdMav5vmkj0AX9lHIND&edm=ALdCaaIBAAAA&ccb=7-5&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-5&oh=00_AT9BXADjYsi8BkryuLhynAbrTCGmgjucZ6CJUxW4VS49QA&oe=62D6BC4F&_nc_sid=bfcd0c",
"is_verified":false,
"has_anonymous_profile_picture":true,
"has_highlight_reels":false,
"account_badges":[
],
"latest_reel_media":0
},
{
"pk":182625349,
"username":"joesmith123",
"full_name":"Joe Smith",
"is_private":false,
"profile_pic_url":"https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/264887407_23285995fef5595973_2438487768932865121_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_cat=105&_nc_ohc=8YpjD2OKeoEAX_gXrh3&edm=APQMUHMBAAAA&ccb=7-5&oh=00_AT9UuhNl9LL_ANffkCcyNNFPv5_yK7J2FKQpRPmqEIri3w&oe=62D60038&_nc_sid=e5d0a6",
"profile_pic_id":"2725348120205619717_182625349",
"is_verified":false,
"has_anonymous_profile_picture":false,
"has_highlight_reels":false,
"account_badges":[
],
"latest_reel_media":0
},
{
"pk":7324707263,
"username":"Mike Jones",
"full_name":"Mike",
"is_private":false,
"profile_pic_url":"https://scontent-lga3-1.cdninstagram.com/v/t51.2885-19/293689497_4676376015169654_5558066294974198168_n.jpg?stp=dst-jpg_s150x150&_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_cat=110&_nc_ohc=ErZ3qsP0LsAAX96OT9a&edm=APQMUHMBAAAA&ccb=7-5&oh=00_AT8pFSsMfJz4Wpq5ulTCpou-4jPs3_GBqIT_SQA6YMaQ0Q&oe=62D61C4D&_nc_sid=e5d0a6",
"profile_pic_id":"28817013445468058864_7324707263",
"is_verified":false,
"has_anonymous_profile_picture":false,
"has_highlight_reels":false,
"account_badges":[
],
"latest_reel_media":1657745524
}
]
}
What I'm trying to do is extract the pk variable from all of them.
I know how to do it on a one by one basis as such:
ids = results['users'][1]['pk']
ids = results['users'][2]['pk']
ids = results['users'][3]['pk']
And so on. But let's say I wanted to extract all of those values in one swoop. I also won't necessarily know how many there are in each JSON object (while the example I used had three, it could be hundreds).
I'm so used to R where you can just doing something like ids = results$pk but don't know how to do this with Python.
EDIT BASED ON rv.kvetch answer
data = results
data = json.loads(data)
ids = [u['pk'] for u in data['users']]
print(ids)
But when I run it I get TypeError: the JSON object must be str, bytes or bytearray, not dict
You can load the json string to a dict object, then use a list comprehension to retrieve a list of ids:
import json
# data is a string
data: str = """
...
"""
data: dict = json.loads(data)
ids = [u['pk'] for u in data['users']]
print(ids)
Output:
[54297756964, 182625349, 7324707263]
I'm trying to get some values from a JSON file in Python to create a curl command with these values from the file.
The values that i am looking for are "alarm-id" and "sequence-id".
The JSON file looks like this:
{
"data": [
{
"attributes": {
"alarm-id": "3672400101833445418",
"sequence-id": "1573135238000"
}
}
]
}
I've tried get() but i cant figure out how to use this correctly.
If you need more information just ask.
Thanks in advance!
Best regards
This should work for you
import json
data = json.loads(#yourJSON)
for attribute in data['data']:
print(attribute['attributes']['alarm-id'])
print(attribute['attributes']['sequence-id'])
You have combination of dictionaries and list. Access dictionary key by name dict["key"] and list element by index.
In short, like this:
>>> d = {"data": [{"attributes":{"alarm-id": "3672400101833445418","sequence-id": "1573135238000"}}]}
>>> d["data"][0]["attributes"]["alarm-id"]
'3672400101833445418'
I have a JSON response from the website shown below. I want to print the 'value' and 'datetime' keys of data. I am not able to access these two elements in JSON response.
data= {"parameter_name":"Inst",
"parameter_code":"WL1","data":[
{"value":3.1289999485,"datetime":"2018-07-01T00:00:00+00:00"},
{"datetime":"2018-07-01T00:30:00+00:00","value":3.1859998703},
{"value":3.33099985123,"datetime":"2018-07-01T00:45:00+00:00"},
{"datetime":"2018-07-01T01:15:00+00:00","value":3.22300004959},
{"datetime":"2018-07-01T01:45:00+00:00","value":3.32299995422}]}
my code till now
for element in len(data['data']):
date = element['datetime']
value = element['value']
print value, date
I am getting error
for element in len(data['data']):
TypeError: string indices must be integers, not str
What you've shown as your JSON data is likely not the actual value of data. If you attempt to access the data like a Python dict, it raises TypeError: string indices must be integers, not str. Your JSON data probably looks like this (notice the quotes):
# This is JSON, essentialy a string in the format of a Python dict.
data = """{
"parameter_name": "Inst",
"parameter_code": "WL1",
"data":[
{
"value":3.1289999485,
"datetime":"2018-07-01T00:00:00+00:00"
},
{
"datetime":"2018-07-01T00:30:00+00:00",
"value":3.1859998703
},
{
"value":3.33099985123,
"datetime":"2018-07-01T00:45:00+00:00"
},
{
"datetime":"2018-07-01T01:15:00+00:00",
"value":3.22300004959
},
{
"datetime":"2018-07-01T01:45:00+00:00",
"value":3.32299995422
}
]
}"""
Convert it into a Python dict by using the Python Standard Library json package:
import json
# This converts the JSON string into a Python dict
data = json.loads(data)
You can access the data and it's 'data' key, then iterate over it (like you were doing):
for element in data['data']:
print(element['value'], element['datetime'])
You can try like this:
for element in data['data']:
date = element['datetime']
value = element['value']
print(date)
print(value)
Output:
3.1289999485
2018-07-01T00:00:00+00:00
3.1859998703
2018-07-01T00:30:00+00:00
3.33099985123
2018-07-01T00:45:00+00:00
3.22300004959
2018-07-01T01:15:00+00:00
3.32299995422
2018-07-01T01:45:00+00:00
Explanation:
If you want to iterate over the elements in the list,:
for element in data['data']
If you want to iterate over the list using by their index:
for index in range(len(data['data'])):
If you have a web responce in text format you would also have to decode it first. Check
https://docs.python.org/2/library/json.html (for python 2) or https://docs.python.org/3.7/library/json.html (for python 3) to see the documentation about the json library.
You have to:
import json
decodedData = json.loads(data)
and then loop over decodedData as you've done.
I was able to use google api finance but it stopped working today so i have been forced to find an alternative.
Im trying to implement google finance module and below is my attempt at it
from googlefinance import getQuotes
import json
import time
import ast
y = json.dumps(getQuotes('YHOO'), indent=2)
print y
print type(y)
print len(y)
price = y[275]
print price
##where i wanna be able to update the stock price
#while True:
#time.sleep(3)
Then i get the following output,
[
{
"Index": "NASDAQ",
"LastTradeWithCurrency": "32.58",
"LastTradeDateTime": "2016-03-03T11:54:19Z",
"LastTradePrice": "32.58",
"LastTradeTime": "11:54AM EST",
"LastTradeDateTimeLong": "Mar 3, 11:54AM EST",
"StockSymbol": "YHOO",
"ID": "658890"
}
]
<type 'str'>
292
[
I know that if i slice it as in y= y[1:len(y)-1] and get rid of [] of the original output, it's in the dict format.
Not sure where to go from here. (I know that if i just copy the output after the slicing and assign it to a new variable, it's stored as a dict )
I'm assuming getQuotes returns a list containing one item which is a dictionary. If you want to get just that dictionary without the list, then extract it using indexing:
my_dict = getQuotes('YHOO')[0]
Because getQotes() returns a list with a dict inside, there is no reason to jsonify it first.
from googlefinance import getQuotes
import json
yhoo = getQuotes('YHOO')[0]
jsonyhoo = json.dumps(yhoo, indent=2)
assert type(yhoo) is dict
assert type(jsonyhoo) is str
As an aside, you don't need to run:
someString[1:len(someString)-1]
You can simply do:
someString[1:-1]
for the same effect.
I would like to print every value that belongs to my id key in my json file. I'm using the below code to print the whole file:
import json
from pprint import pprint
with open('f:\Docs\testList.json') as data_file:
data = json.load(data_file)
pprint( data )
And here is the json
{ "clubs": [
{
"location": "Dallas",
"id": "013325K52",
"type": "bar"
},
{
"location": "Dallas",
"id": "763825X56",
"type": "restaurant"
}
] }
It works correctly, however I can't figure out the type of the data_file and data variables, therefore I have no idea how could I write a for loop to print the content. I'm really new to Python, but in pseudo code I would do something like this (if I assume data is an array (or Python list) of dictionary objects):
for dictionaryVar in jsonArray
print dictionaryVar["id"]
or
for dictionaryVar in jsonArray
if dictionaryVar containsKey: "id"
print dictionaryVar["id"]
I would really appreciate if somebody could show me the right way or give guidance, because I don't really have an idea. I checked the docs of the json module, but couldn't figure out what it really does and how.
data_file is a TextIOWrapper, that is: a file object used for reading text. You should not care about it.
data is a dict. Dictionaries map key-value pairs, but you probably already know that. data has one key, "clubs", which maps to a list. This list contains other dictionaries.
Your pseudo-code:
for dictionaryVar in jsonArray
print dictionaryVar["id"]
corresponds to the following Python code:
for item in data['clubs']:
print item['id']
Your pseudo-code:
for dictionaryVar in jsonArray
if dictionaryVar containsKey: "id"
print dictionaryVar["id"]
corresponds to the following Python code:
for item in data['clubs']:
if 'id' in item:
print item['id']
This should be fairly simple, here is a quite explicit way of doing it (i made it longer so it's clearer for you, you could do this more efficiently)
import json
from pprint import pprint
with open('f:\Docs\testList.json') as data_file:
data = json.load(data_file)
clubs = data['clubs']
for club in clubs:
# Use dict.get() here to default value to None if
# it doesn't exist
club_id = club.get('id', None)
if club_id is not None:
print club_id