Using python to modify format of Json file

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()

Answers


Here it goes. This assumes that your file is just a bunch of individual json objects concatenated and you need to transform in a list of json objects.

import json
from pprint import pprint

with open('sample.json') as data_file:    
    strData = '[' + ''.join(data_file.readlines()).replace('}\n{','},{') + ']'
    data = eval(strData)

with  open('Test.txt', 'w') as f:
    for x in data:
        for y in x["skills"]: 
            f.write(y["name"])

Here are the steps you can take to accomplish your problem. Since it kinda sounds like a homework assignment, I will give you the logic and pointers but not the code.

  1. Open the file for reading
  2. Read file into string variable (if small enough for memory limits)
  3. Create empty list for output
  4. Split string on .....
  5. json.loads each piece of resulting list
  6. Append each result to your empty output list
  7. Have a cup of coffee to celebrate

Need Your Help

object reference not set to an instance of an object C#

c# database exception

I'm trying to write a web application with some database interaction in C#.NET. I'm getting an odd exception that I can't figure out where it's coming from. I found the method that it is coming f...

why does my app complaints about a missing SQL parameter when i already supplied it?

c# sql winforms ado.net

I have a stored procedure that takes 18 parameters and one parameter really annoys me. I have passed a BLOB value of type varbinary(max) an image. By default I pass null as the image. however even ...