I have a data.txt file which I want to convert to a data.json file and print a nice first 2 entries (data.txt contains 3 unique IDs).
The data.txt can oublicly found here (this is a sample - original file contains 10000 unique "linkedin_internal_id).
I tried the following:
with open("data.txt", "r") as f:
content = f.read()
data = json.dumps(content, indent=3)
This code doesn't print the appropriate JSON format of data.txt (it also includes \\). Also, my jupyter notebook gets stacked because of the large file size, for this, I want to nicely print only the first 2 entries.
It is called new line delimited json where each line is a valid JSON value and the line separator is '\n', you can read it like this line by line and push it to a list, so later it will be easy for you to iterate/process it further. See: ldjson
import json
with open("data.txt", "r") as f:
contents = f.read()
data = [json.loads(item) for item in contents.strip().split('\n')]
print(data[0:2])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With