How do you parse this JSON in Python? - python

JSON below
{"result":[
{
"spawn_point_id":"89",
"encounter_id":"1421",
"expiration_timestamp_ms":"1470105387836",
"latitude":38.22,
"longitude": -91.27
},
{
"distance_in_meters":10,
"encounter_id":"9677"
},
{
"distance_in_meters":10,
"encounter_id":"1421"
},
{
"spawn_point_id":"11",
"encounter_id":"2142",
"expiration_timestamp_ms":"1470105387444",
"latitude":38.00,
"longitude": -91.00
}
]}
and i want the output to look like
spawn 89 at lat 38.22 long -91.27
spawn 11 at lat 38.00 long -91.00
i used json.loads and it actually makes the json look funky.
Code so far below:
c = json.loads(r.content)
for d in c['result']:
if d['latitude'] is not None:
print(str(d['latitude']))
seems to kind of work but then get error
Traceback (most recent call last):
File "fast0.py", line 11, in <module>
if d['latitude'] is not None:
KeyError: 'latitude'

You are looking for a key that does not exist. Try:
c = json.loads(r.content)
for d in c['result']:
if 'latitude' in d:
print(str(d['latitude']))

Related

How to parse musixmatch python api JSON response?

How do you parse the JSON response from the musixmatch api?
I'm calling the method
res = artist_api.artist_search_get(format=format, q_artist=artist)
I'm getting a response of
{'message': {'body': {'artist_list': [{'artist': {'artist_alias_list': [],
'artist_comment': '',
'artist_country': '',
'artist_credits': {'artist_list': []},
'artist_edit_url': None,
'artist_id': 26575484.0,
'artist_mbid': None,
'artist_name': 'Illenium',
'artist_name_translation_list': [],
'artist_rating': 55.0, .........
I'm trying to get the artist_id.
I tried getting the artist_id like this:
print(res['message']['body']['artist']['artist_id'])
artist_api = swagger_client.ArtistApi()
format = 'json'
try:
artist_list = ["adele", "lady gaga", "john legend"];
for artist in artist_list:
print(artist)
res = artist_api.artist_search_get(format=format, q_artist=artist)
print(res['message']['body']['artist']['artist_id'])
except ApiException as e:
print "Exception when calling ArtistApi->artist_search_get: %s\n" % e
I'm getting this error message:
Traceback (most recent call last):
File "musixmatch.py", line 39, in <module>
print(res['message']['body']['artist_id'])
TypeError: 'InlineResponse2004' object has no attribute '__getitem__'
Please help I've searched through a bunch of threads but I still can't find the answer to this. I'm new to python so I'm not sure what I'm doing wrong.
You a little messed up with the hierarchy of JSON-document, use this code to get desired values:
[artist['artist']['artist_id'] for artist in res['message']['body']['artist_list']]
As an alternative, can be used JSONPath-expression: $..artist_list..artist_id to get the same result.
Example
from jsonpath_rw import parse
import json
res = json.loads("""{
"message": {
"header": {},
"body": {
"artist_list": [
{
"artist": {
"artist_credits": { },
"artist_country": "string",
"artist_id": 110
}
},
{
"artist": {
"artist_credits": {},
"artist_country": "string",
"artist_id": 220
}
}
]
}
}
}""")
# 1-way: Iterate through JSON-document
print([artist['artist']['artist_id'] for artist in res['message']['body']['artist_list']])
# result: [110, 220]
# 2-way: Use JSONPath-expression
jsonpath_expr = parse('$..artist_list..artist_id')
print([match.value for match in jsonpath_expr.find(res)])
# result: [110, 220]

Getting the date from customText from a json string

I'm trying to loop through this json string
"layoutOptions": {
"titleText": "Route Number",
"authorText": "West LA Yard",
"copyrightText": "",
"customTextElements": [{
"Date": "9/11/2018, 7:37:35 AM"
}
],
"scaleBarOptions": {
"metricUnit": "esriKilometers",
"metricLabel": "km",
"nonMetricUnit": "esriMiles",
"nonMetricLabel": "mi"
},
"legendOptions": {
"operationalLayers": [{
"id": "ParcelRouteEditingTest_1458"
}, {
"id": "ParcelRouteEditingTest_1259"
}
]
}
}
I keep running to this error list indices must be integers, not str
layoutOpsDict = layoutData["layoutOptions"]
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
Error:
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
TypeError: string indices must be integers, not str
What is the best method to grab the date in customTextElements other than keep setting more variables to keep track of?
You are looping through every key instead of just "customTextElements" and not all of them have a list of dictionaries with "Date" as the key.
Since you only want to look through the values mapped to "customTextElements" you can only loop through that:
dateList = [dateEle["Date"] for dateEle in layoutOpsDict["customTextElements"]]

Search JSON file for specific key using python

JSON file:https://1drv.ms/w/s!AizscpxS0QM4hJl99vVfUMvEjgXV3Q
i can extract TECH-XXX
#!/usr/bin/python
import sys
import json
sys.stdout = open('output.txt','wt')
datapath = sys.argv[1]
data = json.load(open(datapath))
for issue in data['issues']:
if len(issue['fields']['subtasks']) == 0:
print(issue['key'])
For every issue without subtasks (TECH-729
TECH-731) i want to extract TECH from
project": {
"avatarUrls": {
"16x16": "https://jira.corp.company.com/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201",
"24x24": "https://jira.corp.company.com/secure/projectavatar?size=small&pid=10001&avatarId=10201",
"32x32": "https://jira.corp.company.com/secure/projectavatar?size=medium&pid=10001&avatarId=10201",
"48x48": "https://jira.corp.company.com/secure/projectavatar?pid=10001&avatarId=10201"
},
"id": "10001",
"key": "TECH",
"name": "Technology",
"self": "https://jira.corp.company.com/rest/api/2/project/10001"
},
and customfield_10107.id
i tried with print(issue['customfield_10107']['id']) and got
./tasks1.py 1.json
Traceback (most recent call last):
File "./tasks1.py", line 11, in <module>
print(issue['customfield_10107']['id'])
KeyError: 'customfield_10107'
key exists under issue and customfield_10107 exists in issue['fields']:
for issue in response["issues"]:
# For issues without subtasks
if len(issue['fields']['subtasks']) == 0:
# Print custom field id
if 'customfield_10107' in issue['fields']:
custom_field = issue['fields']['customfield_10107']
print custom_field['id']
# Print key
if 'key' in issue:
print issue['key']

Returning a JSON float value

I am parsing a JSON file with Python. One of they keys I am trying to parse has a float value, and I am getting the following error: TypeError: list indices must be integers, not str. Below is the code, JSON, and full traceback.
Code:
import json
with open('output.json') as f:
data = json.load(f)
for host in data['ASSET_DATA_REPORT']['HOST_LIST']['HOST']:
print(host['VULN_INFO_LIST']['VULN_INFO']['CVSS_FINAL'])
JSON:
{
"ASSET_DATA_REPORT":{
"HOST_LIST":{
"HOST":[
{
"IP":{
"network_id":"0"
},
"TRACKING_METHOD":"EC2",
"ASSET_TAGS":{
"ASSET_TAG":[
"EC2 Running",
"IF - Database - MySQL"
]
},
"DNS":"i-xxxxxxx",
"EC2_INSTANCE_ID":"i-xxxxxx",
"EC2_INFO":{
"PUBLIC_DNS_NAME":"ec2-xxxxxxxx.amazonaws.com",
"IMAGE_ID":"ami-xxxxxx",
"VPC_ID":"vpc-xxxxxx",
"INSTANCE_STATE":"RUNNING",
"PRIVATE_DNS_NAME":"ip-xxxx.ec2.internal",
"INSTANCE_TYPE":"m3.xlarge"
},
"VULN_INFO_LIST":{
"VULN_INFO":[
{
"CVSS_FINAL":"3.6"
}
]
}
}
]
}
}
}
Traceback:
Traceback (most recent call last):
File "json_format.py", line 11, in <module>
print(host['VULN_INFO_LIST']['VULN_INFO']['CVSS_FINAL'])
TypeError: list indices must be integers, not str
The dictionary containing the "CVSS_FINAL" key is actually itself in a list. Try:
print(host['VULN_INFO_LIST']['VULN_INFO'][0]['CVSS_FINAL'])
As an aside, if you want to store this value as type float in Python (rather than string), you could do:
value = float(host['VULN_INFO_LIST']['VULN_INFO'][0]['CVSS_FINAL'])

Aggregate not returning results, with user authentication in MongoDB

The issue I'm having, I cant make the aggregate search work. I have been able to run it in a mongodb without any users, however in the mongodb with users (even when authentication is not turned on) i get this weird error message (see below). Whats weirder is I have no problem with other functions for example find().count() witch as you can see below returns 32 (the number of results in the database).
Code in MongoDB shell to create user.
use admin
db.createUser( { user: "VibrationDataCollector",
pwd: '45.',
roles: [ "readWriteAnyDatabase",
"dbAdminAnyDatabase",
"clusterAdmin" ] } )
Code in python to conduct search
client = MongoClient('mongodb://xx.x.x.xx:xxxxx/')
db = client['VibrationDataDB']
db.authenticate('VibrationDataCollector', '45.', source='admin')
coll = db.VibrationData
hello = coll.find().count()
print 'count=', hello
LastWrite_Time = coll.aggregate([{
"$unwind": "$Records"
}, {
"$redact": {
"$cond": [{
"$anyElementTrue": {
"$map": {
"input": "$Records.Properties",
"as": "result",
"in": {
"$and": [{
"$eq": ["$$result.Property.Name", "LastWrite_User"]
}, {
"$eq": ["$$result.value", "42"]
}]
}
}
}
},
"$$KEEP",
"$$PRUNE"
]
}
}, {
"$unwind": "$Records.Properties"
}, {
"$match": {
"Records.Properties.Property.Name": 'LastWrite_Time'
}
}, {
"$project": {
"_id": 0,
"value": "$Records.Properties.value"
}
}])
list1 = LastWrite_Time['result']
for T in list1:
print T['value']
Result
count= 32
Traceback (most recent call last):
File "C:/Python_scripts/SimulationCEI.py", line 64, in <module>
list1 = LastWrite_Time['result']
TypeError: 'CommandCursor' object has no attribute '__getitem__'
UPDATEEEEE!!!!
using next()
count= 32
Traceback (most recent call last):
File "C:/Python_scripts/SimulationCEI.py", line 64, in <module>
list1 = LastWrite_Time['result']
TypeError: 'CommandCursor' object has no attribute '__getitem__'
>>> next()
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
next()
TypeError: next expected at least 1 arguments, got 0
>>>
You want:
for T in LastWrite_Time:
print T['value']
"aggregate" returns a cursor that must be iterated. I'm certain that authentication is not related to the error you see.

Categories

Resources