0

I have got the following response after executing res_res = response.json() I have below mentioned sample output with 2 objects and in the actual output there are more objects

    {'restaurants': [{'restaurant':'R': {'has_menu_status': {'delivery': -1,
                                                       'takeaway': -1}},
                             'all_reviews_count': 8,
                             'average_cost_for_two': 350,
                             'book_again_url': '',
                             'book_form_web_view_url': '',
                             'cuisines': 'Momos, Chinese, Fast Food',
                             'name': 'Mumbai Masala'
                             'user_rating': {'aggregate_rating': '3.2',
                                             'rating_color': 'CDD614',
                                             'rating_obj': {'bg_color': {'tint': '500',
                                                                         'type': 'lime'},
                                                            'title': {'text': '3.2'}},
                                             'rating_text': 'Average',
                                             'votes': '9'}}},
                             {'restaurant':'R': {'has_menu_status': {'delivery': -1,
                                                       'takeaway': -1}},
                             'all_reviews_count': 4,
                             'average_cost_for_two': 300,
                             'book_again_url': '',
                             'book_form_web_view_url': '',
                             'cuisines': 'Fast Food',
                             'name' : 'Jumbo King',
                             'user_rating': {'aggregate_rating': '3.4',
                                             'rating_color': 'CDD614',
                                             'rating_obj': {'bg_color': {'tint': '500',
                                                                         'type': 'lime'},
                                                            'title': {'text': '3.4'}},
                                             'rating_text': 'Average',
                                             'votes': '7'}}}],
                            'results_found': 48,
                            'results_shown': 20,
                            'results_start': 0}

I want to extract the just average_cost_for_two and name from every object and store them in seperate list. Could anyone please help me on how to iterate json response i got and get the desired output. Thanks in advance!!!

1 Answer 1

1

To store the result in the separate list (list of dicts, I assume), do:

import json
test = json.loads(test_json) # It is your json from response

separate_list = [{'name': rest['restaurant']['name'], 'average_cost_for_two': rest['restaurant']['average_cost_for_two']}
                 for rest in test['restaurants']]

Then the list will look like:

[{'name': 'Mumbai Masala', 'average_cost_for_two': 350}, {'name': 'Jumbo King', 'average_cost_for_two': 300}]

And you can iterate and access the elements like this:

for element in separate_list:
    print(element['name'])
    print(element['average_cost_for_two'])
Sign up to request clarification or add additional context in comments.

3 Comments

This solution was almost close to the requirement. Actually the above mentioned output was correct as the name, average for two variable are inside the restaurant and not restaurants. Anyway the complete solution is :- separate_list = [{'name': rest['restaurant']['name'], 'average_cost_for_two': rest['restaurant']['average_cost_for_two']} for rest in x['restaurants']]. your solution would have worked completely fine with the output you mention. Thanks for the help.
You're welcome, I fixed an answer ^_^ Yeah, i think theу example above is missing dict brackets after the restaurant, because 'restaurant': 'all_reviews_count': 5 is not a json notation. So i had to make some assumptions about it to test :) Iа everything is solved, can you mark an answer, pls?
Yes....there was a parameter after restaurant. But the parameter was confusing so thought of removing it and you correctly mentioned about the json notation. I will make changes to it. Thanks!!!

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.