Home > Back-end >  How to save a json file using json.dump without the square bracket
How to save a json file using json.dump without the square bracket

Time:11-22

I need to save the json file without the beginning and ending [ and ] respectively.

Sample data:

import pandas as pd
import json

df = pd.DataFrame({'name' : ['abc', 'pqr', 'xzy'],
                  'score' : [85, 90, 80],
                  'address' : ['ab street', 'pq street', 'xy ave']})

df
    name    score   address
0   abc 85  ab      street
1   pqr 90  pq      street
2   xzy 80  xy      ave

I then try to save the above dataframe using:

jl = json.loads(df.to_json(orient='records'))
f = open('expfile.json', 'w')
json.dump(jl, f, indent = 4)
f.close()

Output:

[
    {
        "name": "abc",
        "score": 85,
        "address": "ab street"
    },
    {
        "name": "pqr",
        "score": 90,
        "address": "pq street"
    },
    {
        "name": "xzy",
        "score": 80,
        "address": "xy ave"
    }
]

Which is fine enough, but I need the output without the starting and ending square brackets as below:

{
    "name": "abc",
    "score": 85,
    "address": "ab street"
},
{
    "name": "pqr",
    "score": 90,
    "address": "pq street"
},
{
    "name": "xzy",
    "score": 80,
    "address": "xy ave"
}

Could someone please let me know how to accomplish the same. PS I have complex nested dictionary/json structures inside my columns in many of my dataframes, I parsed them using ast.literal_eval.

I tried using to_json(orient = 'records', lines = True) to which I got this error JSONDecodeError: Extra data: line 2 column 1 (char 425).

CodePudding user response:

The jsoning-in-a-loop variant would be something like this:

jl = [
    {
        "name": "abc",
        "score": 85,
        "address": "ab street"
    },
    {
        "name": "pqr",
        "score": 90,
        "address": "pq street"
    },
    {
        "name": "xzy",
        "score": 80,
        "address": "xy ave"
    }
]

import json
print(",\n".join(json.dumps(x, indent=4) for x in jl))

Produces

{                                                                     
    "name": "abc",                                                        
    "score": 85,                                                          
    "address": "ab street"                                                
},                                                                    
{                                                                     
    "name": "pqr",                                                        
    "score": 90,                                                          
    "address": "pq street"                                                
},                                                                    
{                                                                     
    "name": "xzy",                                                        
    "score": 80,                                                          
    "address": "xy ave"                                                   
}

CodePudding user response:

If want to drop the []'s in output then can iterate over the rows in the dataframe and write out a record at a time.

import pandas as pd
import json

df = pd.DataFrame({'name' : ['abc', 'pqr', 'xzy'],
                  'score' : [85, 90, 80],
                  'address' : ['ab street', 'pq street', 'xy ave']})

with open("out.dat", "w") as fout:
    for idx, row in df.iterrows():
        if idx != 0:
            fout.write(',\n')
        fout.write(json.dumps(row.to_dict(), indent=4))

Output:

{
    "name": "abc",
    "score": 85,
    "address": "ab street"
},
{
    "name": "pqr",
    "score": 90,
    "address": "pq street"
},
{
    "name": "xzy",
    "score": 80,
    "address": "xy ave"
}
  • Related