Reputation: 1092
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"
}
]
}
]
}
]
}
]
Upvotes: 4
Views: 25708
Reputation: 1
You can use the following code:
from google.colab import files
import json
import pandas as pd
import io
print('\x1b[1;31m'+'Please Upload File'+'\x1b[0m')
data = files.upload()
df = pd.read_json(io.BytesIO(data[list(data)[0]]))
Upvotes: 0
Reputation: 56
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.
Hover over the filename, click on the 3 dots next to it and click on 'copy path'.
After this, simply use this code by pasting the file path copied:
df = pd.read_json('file_path_copied')
Upvotes: 1
Reputation: 482
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()
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.
Upvotes: 2
Reputation: 40838
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
Upvotes: 6
Reputation: 1195
JSON handles Unicode strings, not byte sequences. Try:
json.dumps(uploaded.decode("utf-8"))
Upvotes: 1