0

I have a scenario where I am trying to compare list elements with a json file and if there is a match then return certain values and create a json response. here are my json data

[
 {
    "evi": 1223,
    "evn": "testapp1",
    "conf": {
        "c_gr": "tot",
        "c_id": "112"
    }
 },
 {
    "evi": 6759,
    "evn": "testapp2",
    "conf": {
        "c_gr": "tot",
        "c_id": "112"
    }
 },
  {
    "evi": 3352,
    "evn": "testapp3",
    "conf": {
        "c_gr": "tot7",
        "c_id": "112"
    }
 }
]

Here is what I have tried so far :

response=requests.post('https://testaapp.com', headers=headers, data=data)
resp_json=response.json()
if response.status_code == 200:
    print ('working fine...!!')
else:
    print ('notworking !!')
metadata=resp_json['data']
m_list=[1123, 123445, 61887, 3352, 35811488976]
final_lst_data1 = []
final_lst_data2 = []
for key in m_list:
    temp1= key
    for keya in metadata:
        if temp1 in metadata[keya]['evi']:
           final_lst_data1.append(metadata['evn']) #return the names
           final_lst_data2.append(metadata['evn'], metadata['conf']) #return evn and conf values after checking and then dump it to json from list in same json format if not possible then convert to json from list, not sure how to do this here.

But this doesnt works as it gives me below error

    if key in metadata[keya]['evi']:
TypeError: list indices must be integers or slices, not dict
2
  • @Nathan tried for keya in range(len(metadata)) got this error TypeError: argument of type 'int' is not iterable Commented Jan 6, 2020 at 10:20
  • @Nathan please check here for actual son data jsfiddle.net/sa9tLygk Commented Jan 6, 2020 at 10:21

1 Answer 1

1

You are using a dictionary as an index. When you say "for keya in metadata:", keya is a dictionary that refers to the list of dictionaries inside 'metadata'. So, you don't need to use metadata[keya] to access each element, you can just use 'keya'.

for keya in metadata:
    if temp1 == keya['evi']:
       final_lst_data1.append(keya['evn'])
       final_lst_data2.append([keya['evn'], keya['conf']])
Sign up to request clarification or add additional context in comments.

8 Comments

tried the same it gives me error TypeError: append() takes exactly one argument (2 given) under final_lst_data2.append(keya['evn'], keya['conf'])
Also as I had asked in the question that if I need to convert the list response of final_lst_data2 in json how do I do that ?
The error you are getting is because of the last line, you can simply enclose the two arguments inside a list.
As for the json, json.dumps should work fine. That is why I didn't include it in the answer.
I have down the same declared list like this final_lst_data2 = [] and appended like this final_lst_data2.append(keya['evn'], keya['conf']) But still I get the same error can you provide it in answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.