3

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!

Lucien Chardon
  • 451
  • 2
  • 9
  • 15
  • Possible duplicate of [Parsing Firebase JSON with Python](https://stackoverflow.com/questions/49095497/parsing-firebase-json-with-python) – Himanshu Aug 30 '19 at 05:54
  • don't care man, i have the result in 2 lines :) – Batichico Aug 30 '19 at 06:11
  • if it works for you, put as correct answer please :) – Batichico Aug 30 '19 at 07:32
  • Thank you, unfortunately somehow the Firebase json file does not work with the `loads()` command, only with `open()` and `json.load()`. – Lucien Chardon Aug 30 '19 at 10:06
  • man you putted resolved green check in another answer and my answer was answered first and have more likes and is more simply than the other. You ask to change dict with keys to dict without keys. you failed in another thing, but I resolved the real problem . Now i changed loads() to load() , but the real solution is write ```contain = arr.values()``` and ```contain = [*contain]``` I dont know why you didnt give me green check – Batichico Aug 30 '19 at 11:22

3 Answers3

2

the json file is invalid the correct is :

{

    "-LT3ty7Hma7uXWDHmJjH": [
        {
        "id": "-LT3ty7Hma7uXWDHmJjH",
        "message": "jo",
        "time": 1544123048630
        }
    ],
    ......
}
LinPy
  • 16,987
  • 4
  • 43
  • 57
2

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
    }
]
Batichico
  • 676
  • 1
  • 6
  • 15
1

I think you want to convert that json file to a list, you can try:

import json

if __name__ == "__main__":

    json_file = open("log.json")
    dic = json.load(json_file)
    print(dic)

    arr = []

    for item in dic:
        arr.append(dic[item])

    print(arr)