getting specific key from a json format in python - python

I am trying to get a specific key vulnerabilityID and vulnerabilitySoln from this json file. This is towards the bottom of the json script.
{
"vulnerabilities": [
{
"labels": [
{
"labelKey": "com.acme.image.source",
"labelValue": "http://github.com/base-image.git"
},
{
"labelKey": "BA",
"labelValue": "abc"
},
{
"labelKey": "OwnerContact",
"labelValue": "john.doe#gmail.com"
}
],
"repositoryDigest": [
{
"digest": "9089",
"repository": "frost/micro",
"registry": "artifactory.acme.com"
}
],
"artifactorySha": "4324",
"imageID": "er34342",
"createdTimestamp": "2022-10-17",
"engineeringEID": "afd343",
"vulnerabilities": [
{
"test1": "12444",
"test2": "433w",
"vulnerabilityID": "12345",
"vulnerabilitySoln": "http://website.com"
}
]
}
]
}
Here is the code that i attempted
import json
import smtplib
with open("dummy.json") as json_file:
data = json.load(json_file)
def vulnData():
for i in data["vulnerabilities"]:
vulnID = (i["vulnerabilities"]["vulnerabilityID"])
vulnSoln = (i["vulnerabilities"]["vulnerabilitySoln"])
print(vulnID, vulnSoln)
vulnData()
This is the error output I am getting -- TypeError: list indices must be integers or slices, not str. Please suggest any ideas how i can get specific value.
vulnID = (i["vulnerabilities"]["vulnerabilityID"])
TypeError: list indices must be integers or slices, not str

There are also two levels of lists in there:
def vulnData():
for i in data["vulnerabilities"]:
vuln = i["vulnerabilities"][0]
vulnID = vuln["vulnerabilityID"]
vulnSoln = vuln["vulnerabilitySoln"])
print(vulnID, vulnSoln)

Related

How do I return an upper field in a JSON with python?

So, I need some help returning an ID having found a certain string. My JSON looks something like this:
{
"id": "id1"
"field1": {
"subfield1": {
"subrield2": {
"subfield3": {
"subfield4": [
"string1",
"string2",
"string3"
]
}
}
}
}
"id": "id2"
"field1": {
"subfield1": {
"subrield2": {
"subfield3": {
"subfield4": [
"string4",
"string5",
"string6"
]
}
}
}
}
}
Now, I need to get the ID from a certain string, for example:
For "string5" I need to return "id2"
For "string2" I need to return "id1"
In order to find these strings I have used objectpath python module like this: json_Tree.execute('$..subfield4'))
After doing an analysis on a huge amount of strings, I need to return the ones that are meeting my criterias. I have the strings that I need (for example "string3"), but now I have to return the IDs.
Thank you!!
Note: I don't have a lot of experience with coding, I just started a few months ago to work on a project in Python and I have been stuck on this for a while
Making some assumptions about the actual structure of the data as being:
[
{
"id": "id1",
"subfield1": {
"subfield2": {
"subfield3": {
"subfield4": [
"string1",
"string2",
"string3"
]
}
}
}
}
// And so on
]
And assuming that each string1, string2 etc. is in only one id, then you can construct this mapping like so:
data: List[dict] # The json parsed as a list of dicts
string_to_id_mapping = {}
for record in data:
for string in record["subfield1"]["subfield2"]["subfield3"]["subfield4"]:
string_to_id_mapping[string] = record["id"]
assert string_to_id_mapping["string3"] == "id1"
If each string can appear in multiple ids then the following will catch all of them:
from collections import defaultdict
data: List[dict] # The json parsed as a list of dicts
string_to_id_mapping = defaultdict(set)
for record in data:
for string in record["subfield1"]["subfield2"]["subfield3"]["subfield4"]:
string_to_id_mapping[string].add(record["id"])
assert string_to_id_mapping["string3"] == {"id1"}

How to get this json object in python?

so I want to get the first key element from this JSON using python 3.7 without knowing its name.
Here is the JSON:
{
"intent":[
{
"confidence":0.99313362101529,
"value":"sendmessage"
}
],
"wikipedia_search_query":[
{
"suggested":true,
"confidence":0.93804001808167,
"value":"message",
"type":"value"
}
],
"messenger_recipient":[
{
"confidence":0.93138399364195,
"value":"me",
"type":"value"
}
]
}
EDIT:
I want to compare the name of the first key like so:
if(jsonobj[0] == "wikipedia_search_query")
dosomething()
While Python 3.6+ does maintain insertion order on dictionaries, there's no guarantee that your incoming JSON will be in the order you expect. That being said, if you can guarantee the insertion order, here's a working example.
import json
js = """{
"intent":[
{
"confidence":0.99313362101529,
"value":"sendmessage"
}
],
"wikipedia_search_query":[
{
"suggested":true,
"confidence":0.93804001808167,
"value":"message",
"type":"value"
}
],
"messenger_recipient":[
{
"confidence":0.93138399364195,
"value":"me",
"type":"value"
}
]
}"""
json_data = json.loads(js)
first_key = next(iter(json_data))
first_value = json_data[next(iter(json_data))]
print(first_key)
print(first_value)
Output
intent
[{'confidence': 0.99313362101529, 'value': 'sendmessage'}]

Parsing json with python3

Newbie python programmer here, I have the following json response:
[
{
"type": "Incursion",
"state": "mobilizing",
"influence": 1,
"has_boss": true,
"faction_id": 500019,
"constellation_id": 20000739,
"staging_solar_system_id": 30005054,
"infested_solar_systems": [
30005050,
30005051,
30005052,
30005053,
30005054,
30005055
]
},
{
"type": "Incursion",
"state": "established",
"influence": 0,
"has_boss": false,
"faction_id": 500019,
"constellation_id": 20000035,
"staging_solar_system_id": 30000248,
"infested_solar_systems": [
30000244,
30000245,
30000246,
30000247,
30000248,
30000249,
30000250,
30000251,
30000252,
30000253
]
},
{
"type": "Incursion",
"state": "mobilizing",
"influence": 0,
"has_boss": false,
"faction_id": 500019,
"constellation_id": 20000161,
"staging_solar_system_id": 30001101,
"infested_solar_systems": [
30001097,
30001098,
30001099,
30001100,
30001101,
30001102
]
},
{
"type": "Incursion",
"state": "established",
"influence": 0,
"has_boss": false,
"faction_id": 500019,
"constellation_id": 20000647,
"staging_solar_system_id": 30004434,
"infested_solar_systems": [
30004425,
30004426,
30004427,
30004428,
30004429,
30004430,
30004431,
30004432,
30004433,
30004434,
30004435
]
},
{
"type": "Incursion",
"state": "established",
"influence": 0.061500001698732376,
"has_boss": false,
"faction_id": 500019,
"constellation_id": 20000570,
"staging_solar_system_id": 30003910,
"infested_solar_systems": [
30003904,
30003906,
30003908,
30003909,
30003910,
30003903
]
}
]
The original code was written to parse an XML reponse.
This is the code in question:
incursion_constellations = []
if (online):
inc = urllib2.urlopen('https://esi.tech.ccp.is/latest/incursions/')
else:
inc = file(r'incursions.json', 'r')
jinc = json.load(inc)
for j in jinc['items']:
incursion_constellations.append(str(j['constellation']['id_str']))
for s in all_stations:
cur.execute("SELECT constellationID FROM mapSolarSystems WHERE solarSystemID = " + str(s['ssid']))
res = cur.fetchone()
cid = str(res[0])
s['incursion'] = cid in incursion_constellations
The area I have having a hard time understanding is this: for j in jinc['items']:
I am getting this error:
Traceback (most recent call last):
File "./stations.py", line 201, in <module>
for j in jinc['items']:
TypeError: list indices must be integers, not str
Can anyone help me understand how to convert this into being able to parse the json response and retrieve the constellation_id and append it to a list?
Thanks in advance.
Change your original loop to:
for j in jinc:
incursion_constellations.append(str(j['constellation_id']))
But you need to be sure that constellation_id in json is the same id that was under ['constellation']['id_str'] previously
Seeing the [ and ] at the beginning and the end of the response, it seems like this json response is list, not a dict, just as your error is suggesting.
If it is a list, you should be using integer as index, instead of str, like you'd do in dict. Hence, your code should be something like
jinc[0]['constellation_id']
(I don't see where the ['constellation']['id_str'] part comes from)
whatever goes inside the [ and ] is in a list, and should be using an integer index. the ones in { and } are in dict, and should use str index.
to loop through it, just use range and len.
a similar question has been answered here.

string indices must be integers error with json

I am trying to parse out following json using pythong:
{
"document_tone":{
"tone_categories":[
{
"tones":[
{
"score":0.044115,
"tone_id":"anger",
"tone_name":"Anger"
},
{
"score":0.005631,
"tone_id":"disgust",
"tone_name":"Disgust"
},
{
"score":0.013157,
"tone_id":"fear",
"tone_name":"Fear"
},
{
"score":1.0,
"tone_id":"joy",
"tone_name":"Joy"
},
{
"score":0.058781,
"tone_id":"sadness",
"tone_name":"Sadness"
}
],
"category_id":"emotion_tone",
"category_name":"Emotion Tone"
},
{
"tones":[
{
"score":0.0,
"tone_id":"analytical",
"tone_name":"Analytical"
},
{
"score":0.0,
"tone_id":"confident",
"tone_name":"Confident"
},
{
"score":0.0,
"tone_id":"tentative",
"tone_name":"Tentative"
}
],
"category_id":"language_tone",
"category_name":"Language Tone"
},
{
"tones":[
{
"score":0.0,
"tone_id":"openness_big5",
"tone_name":"Openness"
},
{
"score":0.571,
"tone_id":"conscientiousness_big5",
"tone_name":"Conscientiousness"
},
{
"score":0.936,
"tone_id":"extraversion_big5",
"tone_name":"Extraversion"
},
{
"score":0.978,
"tone_id":"agreeableness_big5",
"tone_name":"Agreeableness"
},
{
"score":0.975,
"tone_id":"emotional_range_big5",
"tone_name":"Emotional Range"
}
],
"category_id":"social_tone",
"category_name":"Social Tone"
}
]
}
}
and here is the code that I am trying to use following code to get tone name and score from the json:
import json
from watson_developer_cloud import ToneAnalyzerV3Beta
import urllib.request
import codecs
reader = codecs.getreader("utf-8")
tone_analyzer = ToneAnalyzerV3Beta(
url='https://gateway.watsonplatform.net/tone-analyzer/api',
username='<username>',
password='<password>',
version='2016-02-11')
data=json.dumps(tone_analyzer.tone(text='I am very happy'), indent=2)
print (data)
for cat in data['document_tone']['tone_categories']:
print('Category:', cat['category_name'])
for tone in cat['tones']:
print('-', tone['tone_name'])
but keep running into the error string indices must be integers. I tried to ask the same question in one of my earlier post but with this post I am providing some more details.
I would really appreciate any inputs with this.
Thank You
tone_analyzer.tone(text='I am very happy')
returns a dictionary, there is no need to use json to modify the data in any way, just do
X = tone_analyzer.tone(text='I am very happy')
Note that you have already recieved this exact answer on your previous question.

TypeError: list indices must be integers, not dict

My json file look likes this and I'm trying to access the element syslog in a for loop.
{
"cleanup":{
"folderpath":"/home/FBML7HR/logs",
"logfilename":""
},
"preparation":{
"configuration":{
"src_configfile":"src.cfg",
"dest_configfile":"/var/home/FBML7HR/etc/vxn.cfg"
},
"executable_info1":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/test/hostsim/",
"processname":"northhostsim",
"parameters":"-d"
}
}
],
"executable_info2":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/build/Linux-2.6.18-194.8.1.el5/bin",
"processname":"northhost",
"parameters":"-s brazil -d"
}
}
],
"executable_info3":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"cd /var/home/xxx/SrcCode/vxnservers/fdchost/north/test/vxnclient_mt",
"processname":"vxnclient_north_mt",
"parameters":"0 320 205 14897 16880 60000 60000 2 2"
}
}
]
},
"execution":[
{
"test_case":{
"scriptname":"/var/home/FBML7HR/test/testcase1.sh",
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs1.txt",
"clientsimlog":"/var/home/FBML7HR/test/out.log",
"backupclientsimlog":"Clientlogs1.txt"
}
}
},
{
"test_case":{
"scriptname":"/var/home/FBML7HR/test/testcase2.sh",
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs2.txt",
"clientsimlog":"/var/home/FBML7HR/test/out.log",
"backupclientsimlog":"Clientlogs2.txt"
}
}
}
],
"verification":{
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs.txt",
"reportfilename":"/var/home/FBML7HR/test/out.log",
"backuplogfile":"Clientlogs.txt"
}
}
}
I do it like this:
for i in data['execution']:
cmd = data['execution'][i]['test_case']['scriptname']
But I get the error saying "TypeError: list indices must be integers, not dict".
I'm new to python (and json as well). Could anybody suggest where I'm going wrong ?
You are looping over the values in the list referenced by data['execution'], not indices.
Just use those values (dictionaries) directly:
for i in data['execution']:
cmd = i['test_case']['scriptname']
You probably want to give that a more meaningful loop name:
for entry in data['execution']:
cmd = entry['test_case']['scriptname']
you can try this, it works for me
dataArray=data['execution']
for i in range(len(dataArray)):
cmd = dataArray[i]['test_case']['scriptname']
This exactly loops by indices, so there will not be any confusion,
For me this is very simple and understandable

Categories

Resources