0

I have a data frame df

df:
col1    col2  col3
 1        2     3
 4        5     6
 7        8     9

The json I am looking for is:

 {
            "col1": 1,
            "col1": 4,
            "col1": 7,
        },
        {
            "col2": 2,
            "col2": 5,
            "col2": 8
        },
        {
            "col3": 3,
            "col3": 6,
            "col3": 9,
        }

I have tries df.to_json but its not working

df.to_json(orients=records)
it gives this output
'[{"col1":1,"col2":2,"col3":3},{"col1":4,"col2":5,"col3":6}, 
 {"col1":7,"col2":8,"col3":9}]

This is not the output i was looking for

How to do it in most effective way using pandas/python ?

5
  • 2
    There is typo, need df.to_json(orient='records') Commented Jun 14, 2019 at 6:28
  • no orientS, but orient Commented Jun 14, 2019 at 6:29
  • @jezrael I have tried it but I am not getting expected json I am looking for Commented Jun 14, 2019 at 6:29
  • Check my answer, if lines=True is what you need Commented Jun 14, 2019 at 6:34
  • @manojlds I am getting different output using your code Commented Jun 14, 2019 at 6:38

3 Answers 3

1

JSON files are treated as dicts in python, the JSON file you specified has duplicate keys and could only be parsed as a string (and not using the python json library). The following code:

import json
from io import StringIO

df = pd.DataFrame(np.arange(1,10).reshape((3,3)), columns=['col1','col2','col3'])
io = StringIO()
df.to_json(io, orient='columns')
parsed = json.loads(io.getvalue())
with open("pretty.json", '+w') as of:
    json.dump(parsed, of, indent=4)

will produce the following JSON:

{
    "col1": {
        "0": 1,
        "1": 4,
        "2": 7
    },
    "col2": {
        "0": 2,
        "1": 5,
        "2": 8
    },
    "col3": {
        "0": 3,
        "1": 6,
        "2": 9
    }
}

which you could later load to python. alternatively, this script will produce exatcly the string you want:

with open("exact.json", "w+") as of:
    of.write('[\n\t{\n' + '\t},\n\t{\n'.join(["".join(["\t\t\"%s\": %s,\n"%(c, df[c][i]) for i in df.index]) for c in df.columns])+'\t}\n]')

and the output would be:

[
    {
        "col1": 1,
        "col1": 4,
        "col1": 7,
    },
    {
        "col2": 2,
        "col2": 5,
        "col2": 8,
    },
    {
        "col3": 3,
        "col3": 6,
        "col3": 9,
    }
]

edit: fixed brackets

Sign up to request clarification or add additional context in comments.

Comments

1

You need to do

df.to_json('file.json', orient='records')

Note that this will give you a array of objects:

[
        {
            "col1": 1,
            "col1": 4,
            "col1": 7
        },
        {
            "col2": 2,
            "col2": 5,
            "col2": 8
        },
        {
            "col3": 3,
            "col3": 6,
            "col3": 9
        }
]

You can also do

df.to_json('file.json', orient='records', lines=True)

if you want output like:

{"col1":1,"col1":4,"col1":7},
{"col2":2,"col2":5,"col2":8},
{"col3":3,"col3":6,"col3":9}

To prettify output:

pip install jq
cat file.json | jq '.' > new_file.json

5 Comments

I am getting the output '[{"col1":1,"col2":2,"col3":3},{"col1":4,"col2":5,"col3":6},{"col1":7,"col2":8,"col3":9}]'
@Kallol - try with lines=True. And then you can prettify your json with other tools like jq or your text editor like VSCode
using lines=True I am getting the same output '{"col1":1,"col2":2,"col3":3}\n{"col1":4,"col2":5,"col3":6}\n{"col1":7,"col2":8,"col3":9}'
It's not the same output. But anyway, if you want pretty output, use the jq cli tool
including 'file.json' gives not output, how to find 'file.json'
0

This sort of JSON, whilst valid, is not recommended, and strongly so, as during deserialization, you'll lose all but the last element of your JSON array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.