Thomas
Thomas

Reputation: 13

Manipulating a JSON file for Firebase

I am trying to upload a JSON file to Firebase using python but keep getting the error message: "Key value can't be empty or contain $ # [ ] / or ."

My JSON file(extract bellow) does contain a full stop in each key so I assume that is the problem. My question is if there is a way I could manipulate the JSON file and remove the number followed by the full stop programmatically for all the keys for which there are hundreds of?

"2021-09-24 20:00:00": {
    "1. open": "146.9300",
    "2. high": "147.0100",
    "3. low": "146.9300",
    "4. close": "146.9700",
    "5. volume": "28859"
}

Upvotes: 0

Views: 68

Answers (1)

ManuCF
ManuCF

Reputation: 61

I'll do it this way. Not sure if It's the best, but It works ;-)

from json import loads

json_data = \
"""
{
"2021-09-24 20:00:00": {
    "1. open": "146.9300",
    "2. high": "147.0100",
    "3. low": "146.9300",
    "4. close": "146.9700",
    "5. volume": "28859"
}
}
"""
data = dict(loads(json_data))

for date in data:
    for field in data[date]:
        valueString = data[date][field]
        intValue = valueString.split('.')[0]
        data[date][field] = intValue

print(data)

Output:

{"2021-09-24 20:00:00": {"1. open": "146", "2. high": "147", "3. low": "146", "4. close": "146", "5. volume": "28859"}}

Upvotes: 1

Related Questions