Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access uploaded json file google colab

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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
like image 252
ekclone Avatar asked Oct 21 '25 15:10

ekclone


1 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

like image 164
korakot Avatar answered Oct 24 '25 05:10

korakot