Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Newline separated JSON instead of comma separated

I am getting some data from an API, that I want to then import into a BigQuery table. The doc specifies :

JSON data must be newline delimited. Each JSON object must be on a separate line in the file.

And I cannot seem to find a way to convert my JSON.

Here is my code, getting the first 2 lines of the API response and printing them :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    cols = ["id","email","surname"]

    for line in response.text.split("\n")[1:3]:
        obj={}
        for name,value in zip(cols,line.split(";")):
            obj[name] = value
        data.append(obj)

    print(json.dumps(data))

The output is the following :

[{"id": "1", "email": "randommail@provider.fr", "surname": "Alice"}, {"id": "2", "email": "randommail@gmail.com", "surname": "Bob"}]

How should I proceed to format the JSON like this ?

{"id": "1", "email": "randommail@provider.fr", "surname": "Alice"}
{"id": "2", "email": "randommail@gmail.com", "surname": "Bob"}

Thanks in advance

>Solution :

Writing an answer since comments are limited regarding formatting. Like you wrote, you should handle each dictionary as a separated json:

output = ''
cols = ["id","email","surname"]
    for line in response.text.split("\n")[1:3]:
        obj={}
        for name,value in zip(cols,line.split(";")):
            obj[name] = value
        output += json.dumps(obj) + '\n'
print(output)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading