2

I have a json file like that:

{
    "info": [
        {
            "product": "ABC",
            "email-adr": "[email protected]",
            "Gender": "M",
            "Name": "João",
            "id": "1069",
            "mobile": "iOS | 9.1",
            "client": " "
        },

I tried to:

data2 = pd.read_json('file.json', encoding=str)

but got a df with a single column:

                      info
0  {u'id':u'1069',u'client'..    

What is the best way to read this json file to a pandas df?

1 Answer 1

1

You have to pass as argument only the inner object:

import json

z="""{
"info":
[
{"product": "ABC", "email-adr": "[email protected]", "Gender": "M", "Name": "João", "id": "1069", "mobile": "iOS | 9.1", "client": " " }]}"""
>>> pd.DataFrame(json.loads(z)['info'])

  Gender  Name client        email-adr    id     mobile product
0      M  João         [email protected]  1069  iOS | 9.1     ABC

To load a json file:

with open("file.json", "r") as f:
  data = json.load(f)

Then you can do

pd.DataFrame(data['info'])
Sign up to request clarification or add additional context in comments.

2 Comments

as I have several lines in this file, I tried: df=pd.DataFrame(json.loads('file.json')['info']) and received the following error: ValueError: No JSON object could be decoded
This is not how you load a json file. Let me edit and show you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.