'Data from GET is returned differently then how the data is stored for map values
For example, here's how my data is stored:
When I do a GET for the data from this URL (using the REST API):
https://<redacted>.com/maps/a6c00d68a95a5043374c1017b982d8f0b3009179/data/003_teleportIns.json
The result looks like this:
[{"sequenceNumber":0,"x":8,"y":3},{"sequenceNumber":1,"x":3,"y":14},{"sequenceNumber":2,"x":11,"y":15}]
What I expected, was for the data to look like this instead:
{"0":{"sequenceNumber":0,"x":8,"y":3},"1":{"sequenceNumber":1,"x":3,"y":14},"2":{"sequenceNumber":2,"x":11,"y":15}}
Since I'm using kotlinx serialization to decode the data, the way that firebase returns the data results in errors when trying to decode.
Here's an example error:
Unexpected JSON token at offset 199: Expected start of the object '{', but had ':' instead
JSON input: .....":5,"y":7}],
"003_teleportIns":[{"sequenceNumber":0,"x":8,"y.....
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 199: Expected start of the object '{', but had ':' instead
JSON input: .....":5,"y":7}],
"003_teleportIns":[{"sequenceNumber":0,"x":8,"y.....
So what gives? Why is firebase returning my data in an unexpected manor? If this is expected, please point me to the documentation.
Solution 1:[1]
If you have sequential numeric keys in the result, the Firebase REST API (and SDKs) returns those as an array. There is no way to change this behavior, so what I usually do is prefix those keys with a short non-numeric string to prevent the array coercion. So "key_0", "key_1", etc.
Also see Best Practices: Arrays in Firebase.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |

