Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load data exported from Firebase as json into Python script

I have exported some log data from a Firebase app to a json file. The file structure looks like this:

{
    "-LT3ty7Hma7uXWDHmJjH": {
        "id": "-LT3ty7Hma7uXWDHmJjH",
        "message": "jo",
        "time": 1544123048630
    },
    "-LT3tzmgUQkHJaaYBY6d": {
        "id": "-LT3tzmgUQkHJaaYBY6d",
        "message": "bla bla bla",
        "time": 1544123055429
    },
    ...
}

Problem: When i use this code ...

import json

arr = json.loads("log.json")
print(arr)

... i get the error message json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0). I tried replacing the curly braces at the very end with square brackets to make it an array, same error.

Goal: In the end, want to get a python list of dictionaries like this:

[

    {
        "id": "-LT3ty7Hma7uXWDHmJjH",
        "message": "jo",
        "time": 1544123048630
    },
    {
        "id": "-LT3tzmgUQkHJaaYBY6d",
        "message": "bla bla bla",
        "time": 1544123055429
    },
    ...
]

What am i doing wrong?

Thanks or your help!

like image 971
Lucien Chardon Avatar asked Jan 01 '26 03:01

Lucien Chardon


1 Answers

You have the result in 2 lines, try it :) :

import json

arr = json.load("log.json")
print(arr)

contain = arr.values()
contain = [*contain]
print(contain)

Console:

[
    {
        'id': '-LT3ty7Hma7uXWDHmJjH', 
        'message': 'jo', 
        'time': 1544123048630
    }, 
    {
        'id': '-LT3tzmgUQkHJaaYBY6d', 
        'message': 'bla bla bla', 
        'time': 1544123055429
    }
]
like image 66
Batichico Avatar answered Jan 03 '26 15:01

Batichico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!