gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Write a python script that will open a JSON file and read the field 'id' from all of the…
- User
Write a python script that will open a JSON file and read the field 'id' from all of the json objects and saves them into a newline delimited file
- Assistant
Here's a Python script that opens a JSON file, reads the 'id' field from all JSON objects, and saves them into a newline delimited file:
pythonimport json def extract_ids(json_file, output_file): with open(json_file, 'r') as f: data = json.load(f) # Extract 'id' field from each JSON object ids = [item['id'] for item in data] with open(output_file, 'w') as f: # Save ids into newline delimited file f.write('\n'.join(ids)) # Usage example: json_file = 'data.json' output_file = 'ids.txt' extract_ids(json_file, output_file)Make sure to replace 'data.json' with the path to your JSON file, and 'ids.txt' with the desired output file name.