Your code creates new dictionary object for each object with:
Copymy_dict={}
Moreover, it overwrites the previous contents of the variable. Old dictionary in m_dict is deleted from memory.
Try to create a list before your for loop and store the result there.
Copyresult = []
for item in json_decode:
my_dict={}
my_dict['title']=item.get('labels').get('en').get('value')
my_dict['description']=item.get('descriptions').get('en').get('value')
my_dict['id']=item.get('id')
print(my_dict)
result.append(my_dict)
Finally, write the result to the output:
Copyback_json=json.dumps(result)
Printing the dictionary object aims to help the developer by showing the type of the data. In u'Diego Vel\xe1zquez', u at the start indicates a Unicode object (string). When object using is printed, it is decoded according to current language settings in your OS.
Answer from jms on Stack OverflowYour code creates new dictionary object for each object with:
Copymy_dict={}
Moreover, it overwrites the previous contents of the variable. Old dictionary in m_dict is deleted from memory.
Try to create a list before your for loop and store the result there.
Copyresult = []
for item in json_decode:
my_dict={}
my_dict['title']=item.get('labels').get('en').get('value')
my_dict['description']=item.get('descriptions').get('en').get('value')
my_dict['id']=item.get('id')
print(my_dict)
result.append(my_dict)
Finally, write the result to the output:
Copyback_json=json.dumps(result)
Printing the dictionary object aims to help the developer by showing the type of the data. In u'Diego Vel\xe1zquez', u at the start indicates a Unicode object (string). When object using is printed, it is decoded according to current language settings in your OS.
When you do this:
Copyfor item in json_decode:
You are looping through each line in the file.
Every time through the loop you are overriding the my_dict variable, which is why you get only one line in your output.
Once you load in the file, you can simply print out the json_decode variable to do what you want.
https://docs.python.org/3.3/library/json.html
How to parse different part of json file (Newbie here)
How to transform a JSON file into a CSV one in Python?
python - Extract single value from JSON data using key - Stack Overflow
Traverse through multiple folder/subfolders and extract Json data and store into CSV using python
Videos
I have a large JSON file with multiple JSON objects. Each object should contain data that includes "sounds" and "pos". That is, for each object, there is a section called "sounds" which contains things like IPA and accent tags, and a section called "pos" which contains parts of speech. I am trying to extract the "sounds" and "pos" sections for each object from the file. I am very new to python, so I am unsure of what I'm doing wrong. When I run the below code, it prints "None" many times.
import json def extract_specific_data_from_entries(json_file, keys): extracted_data_list = [] with open(json_file, 'r', encoding='utf-8') as file: for line in file: data = json.loads(line.strip()) extracted_data = extract_specific_data(data, keys)
extracted_data_list.append(extracted_data) return extracted_data_list
def extract_specific_data(data, keys):
extracted_data = data
for key in keys:
if isinstance(extracted_data, dict):
extracted_data = extracted_data.get(key)
elif isinstance(extracted_data, list):
try:
key = int(key)
extracted_data = extracted_data[key]
except (ValueError, IndexError):
extracted_data = None
else:
extracted_data = None
break
return extracted_data
if name == "main":
json_file = "kaikki.org-dictionary-English.json"
keys = ["sounds", "pos"]
extracted_data_list = extract_specific_data_from_entries(json_file, keys) print(extracted_data_list)