I have a JSON file that looks like this:
20219
{"topic":"electronics","question":"What is the effective differencial effective of this circuit","excerpt":"I'm trying to work out, in general terms, the effective capacitance of this circuit (see diagram: https://i.sstatic.net/BS85b.png). \n\nWhat is the effective capacitance of this circuit and will the ...\r\n "}
{"topic":"electronics","question":"Heat sensor with fan cooling","excerpt":"Can I know which component senses heat or acts as heat sensor in the following circuit?\nIn the given diagram, it is said that the 4148 diode acts as the sensor. But basically it is a zener diode and ...\r\n "}
{"topic":"electronics","question":"Outlet Installation--more wires than my new outlet can use [on hold]","excerpt":"I am replacing a wall outlet with a Cooper Wiring USB outlet (TR7745). The new outlet has 3 wires coming out of it--a black, a white, and a green. Each one needs to be attached with a wire nut to ...\r\n "}
{"topic":"electronics","question":"Buck Converter Operation Question","excerpt":"i have been reading about the buck converter, and have also referred to the various online resources like here.\n\n\n\nIn the above circuit, as I understand, when switch closes, current starts to increase ...\r\n "}
{"topic":"electronics","question":"Urgent help in area of ASIC design, verification, SoC [on hold]","excerpt":"I need help with deciding on a Master's Project and I need some ideas related to the field of ASIC Design/ verification or something related to SoC's, FPGA and or combination. I wish to pursue the ...\r\n "}
The first line is a number (20219), which is basically number of records in the file is followed by the data. I tried using the following:
import json
with open('training_json.json') as data_file:
ndocs = json.readlines(data_file)[0]
and
with open('training_json.json') as data_file:
next(data_file)
docs = json.load(data_file)
but it couldn't get through. Any ideas how I can read the number on the first line and the data trailing below in different objects?
Read the first line, then send everything else for parsing to json.loads():
with open("training_json.json") as data_file:
number = next(data_file).strip()
your_json = json.loads(data_file.read())
In case you have a different JSON object on each line (as it appears from your image), then read the rest line-by-line and store in a list:
with open("training_json.json") as data_file:
number = next(data_file).strip()
your_jsons = [json.loads(line) for line in data_file]
If your JSON is broken down on lines haphazzardly, you'll have to do some manual reconstruction before you get it to parse.
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