How to get a dictionary from a json file in Python?
dictionary, json, python
Solution
json_data is a dictionary. In your first loop, you're iterating over a list of the dictionary's keys:
for category in json_data:
category will contain key strings - General and Acciones politicas.
You need to replace this loop, which iterates over keys' letters:
for trendingtopic in category:
with the below, so that it iterates over the dictionary elements:
for trendingtopic in json_data[category]:
Problem
I got this code for implementing my needing: ``` import json json_data = [] with open("trendingtopics.json") as json_file: json_data = json.load(json_file) for category in json_data: print category for trendingtopic in category: print trendingtopic ``` And this is my json file: ``` { "General": ["EPN","Peña Nieto", "México","PresidenciaMX"], "Acciones politicas": ["Reforma Fiscal", "Reforma Energética"] } ``` However I'm getting this printed: ``` Acciones politicas A c c i o n e s p o l i t i c a s General G e n e r a l ``` I want to get a dictionary being Strings the keys and got a list as value. Then iterate over it. How can I accomplish it?