2

I need to convert an unconventional json file into a dataframe. I can loop through and do it. However, I was just wondering if there are other efficient ways of doing the same thing. The json I have is of the following format

{"K1": "V11", "K2": "V12", "K3": "V13"}
{"K1": "V21", "K2": "V22", "K3": "V23"}
{"K1": "V31", "K2": "V32", "K3": "V33"}

I want to read it as a dataframe as follows

K1   K2   K3
V11  V12  V13
V21  V22  V23
V31  V32  V33

Are there any streaming Json packages or any easy approach to do this. Any help would be great.

2
  • 2
    That is more like a dict to me , not json Commented Sep 9, 2019 at 15:42
  • Absolutely right @WeNYoBen. But this is the way the source data is structured which is actually a log file. Can you please help with this? What would be the best way to create a dataframe from such a file? I can also save the source file as .txt if required. Commented Sep 9, 2019 at 15:49

2 Answers 2

4

Starting from Pandas 0.19, read_json has native support for JSON in multiple lines

pd.read_json(jsonfile, lines=True)

where jsonfile is:

{"K1": "V11", "K2": "V12", "K3": "V13"}
{"K1": "V21", "K2": "V22", "K3": "V23"}
{"K1": "V31", "K2": "V32", "K3": "V33"}

from doc,

jsonfile : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json

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

1 Comment

Works perfectly! Thanks
0

I put your data in a file named jsonlike.txt

import pandas

the_df = pandas.read_json("jsonlike.txt", "columns", lines=True)
print(the_df)

output:
    K1   K2   K3
0  V11  V12  V13
1  V21  V22  V23
2  V31  V32  V33
3  V11  V12  V13
4  V21  V22  V23
5  V31  V32  V33
6  V11  V12  V13
7  V21  V22  V23
8  V31  V32  V33

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.