How to access uploaded json file google colab

Viewed 11411

I'm stuck trying to read the files in google colab, It should read the file as a simple JSON but I can't even do a json.dumps(file) without getting 100 of errors

Uploading the file:

import json 
import csv 
from google.colab import files
uploaded = files.upload()

Printing works, It shows the content of the file:

print(uploaded)
data = json.dumps(uploaded)

But I get Object of type 'bytes' is not JSON serializable when trying to do json.dumps(uploaded)

Shouldn't the file be read as json and not bytes? In some other cases, I tested it also read as dictionary

JSON file:

[
    {
        "type": "message",
        "subtype": "channel_join",
        "ts": "123",
        "user": "DWADAWD",
        "text": "<@DWADAWD> has joined the channel"
    },
    {
        "type": "message",
        "subtype": "channel_join",
        "ts": "123",
        "user": "DWADAWD",
        "text": "<@DWADAWD> has joined the channel"
    },
    {
        "text": "Let's chat",
        "user_profile": {
            "display_name": "XASD",
            "team": "TDF31231",
            "name": "XASD",
            "is_restricted": false,
            "is_ultra_restricted": false
        },
        "blocks": [
            {
                "type": "rich_text",
                "block_id": "2N1",
                "elements": [
                    {
                        "type": "rich_text_section",
                        "elements": [
                            {
                                "type": "text",
                                "text": "Let's chat"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
3 Answers

If you upload just 1 file. You can get the content from its values()

data = next(iter(uploaded.values()))

Then, you can convert json string to dict

d = json.loads(data.decode())

Here's an example notebook

JSON handles Unicode strings, not byte sequences. Try:

json.dumps(uploaded.decode("utf-8"))

I prefer to use io and files.

First, I import them (and pandas):

import io
import pandas as pd
from google.colab import files

Then, I use a file widget to upload the file:

uploaded = files.upload()

enter image description here

To load the data into a dataframe:

df = pd.read_json(io.StringIO(uploaded.get('file.json').decode('utf-8')))

The dataframe df has all json data.

Related