Muhammad Yasir Javed
Muhammad Yasir Javed

Reputation: 97

Add a COMMA after a CURLY Brace For JSON Format

Hello there i am working on a dataset but its not formatted correctly. It's missing its Square brackets and comma after every object Example:

{"is_sarcastic": 1, "headline": "thirtysomething scientists unveil doomsday clock of hair loss", "article_link": "something"}
{"is_sarcastic": 0, "headline": "dem rep. totally nails why congress is falling short on gender, racial equality", "article_link": "somethingelse"}

I want to format it such that it turns to this:

[{"is_sarcastic": 1, "headline": "thirtysomething scientists unveil doomsday clock of hair loss", "article_link": "something"},
{"is_sarcastic": 0, "headline": "dem rep. totally nails why congress is falling short on gender, racial equality", "article_link": "somethingelse"}]

I am using Python 3.x to achieve this task.

Upvotes: 0

Views: 1058

Answers (1)

Khaled Khairy
Khaled Khairy

Reputation: 41

you can run the following python script, it'll output a file containing the output you desire.

import json

dataJson = []

with open('data.json') as f:
    for jsonObj in f:
        dataDict = json.loads(jsonObj)
        dataJson.append(dataDict)

#print (dataJson)        
with open('data2.json', 'w') as jsonfile:
    json.dump(dataJson, jsonfile)

where data.json is the name of the file containing the dataset

Upvotes: 4

Related Questions