How to access uploaded json file google colab

Question:

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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
Asked By: ekclone

||

Answers:

JSON handles Unicode strings, not byte sequences. Try:

json.dumps(uploaded.decode("utf-8"))
Answered By: CarlosSR

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

Answered By: korakot

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.

Answered By: neosergio

Another way to do this is by uploading your json files to Colab and then copying their path as the filename.

Here’s how it is done:

import io
import pandas as pd
from google.colab import files
uploaded = files.upload()

This is where you’d upload your files.

Now if you click on the following folder icon on the left side of your code on Colab, you should see your uploaded files.
enter image description here

Hover over the filename, click on the 3 dots next to it and click on ‘copy path’.

enter image description here

After this, simply use this code by pasting the file path copied:

df = pd.read_json('file_path_copied')
Answered By: Navya Jain
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.