krock1516
krock1516

Reputation: 461

How to get the specific index from a json list data

We have Json Data which i'm trying to parse, While reading the data via url with json & resquest.

Sample Jason Data:

{"statusMessage": "OK", "statusCode": 200, "response": { "id": "15015", "name": "cDOT_stx7116_esx01_07-18-2021_18.00.00.0586", "startTime": 1626624000472, "mounted": false, "vmwareSnapshot": "Yes", "status": "Completed", "policy": "SCV_DLY_NoDR", "entities": [ { "entityName": "CISCAT-2016_CN-SKK", "quiesced": true, "uuid": "500334f5-fea6-2992-a543-e4bd56822913", "locations": [ "[std7116_esx01] CISCAT-2016_CN-SKK/CISCAT-2016_.vmtx" ] }, { "entityName": "ind4481", "quiesced": true, "uuid": "500ae9dd-60e7-2339-4c4c-557ba00a3696", "locations": [ "[stx7116_esx01] ind4481/ind4481.vmx" ] }}

What I tried:

response = requests.get('https://str001.example.com:8144/api/4.1/backups/'+y1, headers=headers, verify =False)
output = response.json()
data4 = output['response']['entities']

Below is what i'm trying to do with a For loop and trying to get the desired index but it did not worked:

for k in data4:
    x1 = (k['locations'].rsplit("]", 3)[-3].split("[")[-1],k['entityName'],k['quiesced'])
    print (x1)

Desired output

stx7116_esx01 CISCAT-2016_CN-SKK true

Upvotes: 0

Views: 55

Answers (1)

Ajay Singh Rana
Ajay Singh Rana

Reputation: 573

If the json object that you have provided is exact then you might try:

locations = []
for i in range(0,len(data4),1):
    data_dict = data4[i]     # each element is a separate dictionary
    location = data_dict['locations'][0]    # as locations is a list with a sngle item you'll have to extract it this way
    location =  location[location.find('[') + 1 : location.finct(']')]    # slice the string on range of the indexes of '[' and ']'
    desired_output = location + ' ' + data_dict['entityName'] + ' ' + data_dict['quiesced']

Upvotes: 1

Related Questions