Ranjith N
Ranjith N

Reputation: 91

how to make my json formatted using python

i have json that looks like this:

{
    "message": ".replace(commentRegExp, '')",
    "report_id": 1961272
}{
    "message": ".replace(currDirRegExp, '')",
    "report_id": 1961269
}{
    "message": ".replace(jsSuffixRegExp, '');",
    "report_id": 1961270
}

how to make it into correct format using python i want the json data to look like this:

[
 {
    "message": ".replace(commentRegExp, '')",
    "report_id": 1961272
 },
 {
    "message": ".replace(currDirRegExp, '')",
    "report_id": 1961269
 },
 {
    "message": ".replace(jsSuffixRegExp, '');",
    "report_id": 1961270
 }
]

Upvotes: -1

Views: 156

Answers (4)

peak
peak

Reputation: 116690

The following is a generic solution for reading a stream of JSON texts. They need not be new-line delimited. It is, however, assumed that is on your path.

For illustration, the JSON objects shown in the question are also assumed to be in a file named 'json.txt'.

import json
import sh

infile='json.txt'
cmd = sh.jq('-M', '-s', '.', infile)
obj = json.loads( cmd.stdout )
print( json.dumps(obj, indent=2) )

This produces the desired output.

(For testing, you could run: jq -s . infile)

Upvotes: 1

peak
peak

Reputation: 116690

This python3 script shows how to read a stream of JSON entities in a file, and how to "slurp" them into an array, using only the following two headers:

import json
from splitstream import splitfile

infile='json.txt'

# Assuming filename contains a stream of JSON texts,
# this function returns each as a Python string 
# that can be read using json.loads(_)
def stream(filename):
    with open(filename, 'r') as f:
        for s in splitfile(f, format="json"):
            yield s

obj = []
for jstr in stream(infile):
    obj += [ json.loads(jstr) ]

print( json.dumps( obj ) )

Output

[{"message": ".replace(commentRegExp, '')", "report_id": 1961272}, {"message": ".replace(currDirRegExp, '')", "report_id": 1961269}, {"message": ".replace(jsSuffixRegExp, '');", "report_id": 1961270}]

Formatted Output

$ python3 slurpfile.py | jq .
[
  {
    "message": ".replace(commentRegExp, '')",
    "report_id": 1961272
  },
  {
    "message": ".replace(currDirRegExp, '')",
    "report_id": 1961269
  },
  {
    "message": ".replace(jsSuffixRegExp, '');",
    "report_id": 1961270
  }
]

Upvotes: 0

peak
peak

Reputation: 116690

The following uses the "pip install jq" module: https://pypi.org/project/jq/

import json
from jq import jq  # jq(CMD).transform(DATA)

infile='json.txt'

def input(filename):
    with open(filename, 'r') as f:
        return f.read()

str = input( infile ); 

print( jq(".").transform(text=str, multiple_output=True))

Output

The above produces:

[{'message': ".replace(commentRegExp, '')", 'report_id': 1961272}, {'message': ".replace(currDirRegExp, '')", 'report_id': 1961269}, {'message': ".replace(jsSuffixRegExp, '');", 'report_id': 1961270}]

JSON output

To produce JSON output:

print(json.loads(json.dumps(jq(".").transform(text=str, multiple_output=True) )))

Upvotes: 0

Tom
Tom

Reputation: 725

Something like this will split up the root elements

import json
import re

json = '{"message":".replace(commentRegExp, '')","report_id":1961272}{"message":".replace(currDirRegExp, '')","report_id":1961269}{"message":".replace(jsSuffixRegExp, '');","report_id":1961270}'

match_array = re.findall("[{].*?[}]", json)

json_new = ""

for x in match_array:
    json_new+=(x+",")   

json_new = "["+json_new[:-1]+"]"

Edit to Read from file;

import json
import re

with open('test.json', 'r') as myfile:
    data=re.sub(r"[\n\t\s]*", "", myfile.read())

match_array = re.findall("[{].*?[}]", data)

json_new = ""

for x in match_array:
    json_new+=(x+",")   

json_new = "["+json_new[:-1]+"]"

print(json_new)

The Bulk of what this solution is doing is based around the [{].*?[}] regex which will find all the json root elements then comma separate them and append square brackets on the start and end

Upvotes: -1

Related Questions