Expecting JSON from firebase but getting a list

Viewed 64

I am using python-firebase library for accessing firebase realtime database. The structure of the database is depicted in this image. When I am accessing the node "PatientMaster" using python, I am getting a list of JSONs without the ID's instead of getting a proper JSON key-value format. Could someone please help me with this? The code which I have written for doing the above is:

firebase = firebase.FirebaseApplication('https://************.firebaseio.com', None)
result = firebase.get("/PatientMaster",None)

The variable "result" gives me the output as a list of JSONs without ID's.

1 Answers

The problem is that you are using sequential numeric keys for the records in node 'PatientMaster' and the record contents are very similar. Firebase RTDB interprets (sometimes) that structure as a list array rather than a map array. The reason being that when you post a list array to FB RTDB it stores it as a map (json) with keys 0-n and the list items as the values. When you read the db and it sees this structure it thinks it is a stored list array and returns that instead of a map (json). See this post: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html?m=1

You need to make your keys more complex, or test / expect the data returned from this node to be a list array.

Related