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!

5
  • Possible duplicate of Parsing Firebase JSON with Python Commented Aug 30, 2019 at 5:54
  • don't care man, i have the result in 2 lines :) Commented Aug 30, 2019 at 6:11
  • if it works for you, put as correct answer please :) Commented Aug 30, 2019 at 7:32
  • Thank you, unfortunately somehow the Firebase json file does not work with the loads() command, only with open() and json.load(). Commented Aug 30, 2019 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 Commented Aug 30, 2019 at 11:22

3 Answers 3

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
    }
]
Sign up to request clarification or add additional context in comments.

Comments

2

the json file is invalid the correct is :

{

    "-LT3ty7Hma7uXWDHmJjH": [
        {
        "id": "-LT3ty7Hma7uXWDHmJjH",
        "message": "jo",
        "time": 1544123048630
        }
    ],
    ......
}

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.