I have JSON file that is formatted like this: (multi-line for clarity)
(line 0001).......
{
"_id": "iD_0001",
"skills": [{
"name": "Project Management"
}, {
"name": "Business Development"
}]
}
.... (line 9999)
{
"_id":"iD_9999",
"skills": [{
"name": "Negotiation"
}, {
"name": "Banking"
}]
}
I'd like to run a program on it, however, the program cannot read it under the aforementioned format. Thus I'd like to modify its format to:
[{
"_id": "iD_0001",
"skills": [{
"name": "Project Management"
}, {
"name": "Business Development"
}]
},{
"_id":"iD_9999",
"skills": [{
"name": "Negotiation"
}, {
"name": "Banking"
}]
}]
Essentially, putting all entries in a single array. Is there a way to implement that using Python or demjson?
ALTERNATIVE: I made a program that fetches the skills in these json files and sends them to a text file (Test.txt), however it only works for the second format, not the first. Can you suggest a modification to make it work for the first format (above)? This is my program:
import json
from pprint import pprint
with open('Sample.json') as data_file:
data = json.load(data_file)
with open('Test.txt', 'w') as f:
for x in data:
for y in x["skills"]:
f.write(y["name"])
f.close()
SOLUTION
Thank you to Antti Haapala for noticing the catenation of Json objects under the first format, as well as to Walter Witzel and Josh J for suggesting alternative answers. Since the first format is a catenation of individual objects, the program functions well if we load the first Json file Line-by-Line instead of as a whole. I have done that with:
data = []
with open('Sample1-candidats.json') as data_file:
for line in data_file:
data.append(json.loads(line))
with open('Test.txt', 'w') as f:
for x in data:
for y in x["skills"]:
f.write(y["name"])
f.close()