2

Firebase is organizing an imported JSON file in the following way:

Firebase database view

But the imported file (and exported file from Firebase) is organized this way:

 {
  "features" : [ {
    "geometry" : {
      "coordinates" : [ -77.347191, 36.269321 ],
      "type" : "Point"
    },
    "properties" : {
      "name" : "Branch Chapel",
      "osm_id" : "262661",
      "religion" : "christian"
    },
    "type" : "Feature"
  },
...

It appears that Firebase assigns an internal number to each object in the array of "features". This is nice, but it makes it hard to reference each object without knowing how Firebase is naming it- and I have 400k+ objects.

Is there a way to assign an id to each object to prevent Firebase from generating its own? Or is there a way to programmatically rename/reorganize the data after it's been imported? The optimal outcome would have the object named by its osm_id, rather than some arbitrary number Firebase assigns.

Any help is appreciated.

6
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in your Firebase Database console. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. Commented Feb 21, 2017 at 23:48
  • The 0 and 1 keys that Firebase uses for your array are not arbitrary. See firebase.googleblog.com/2014/04/… Commented Feb 21, 2017 at 23:51
  • Thanks. I'm mainly interested in the "99099"- it's not found in the JSON document, so I'm assuming Firebase assigned it in order of processing. Is there a way to edit this value at the time of import, by editing the document- or would it be better to update it after it's in the database? Commented Feb 22, 2017 at 3:56
  • Firebase might have detected an array there at some point. It's hard to say without seeing the code that got this data in the first place. But either way, the simplest solution to prevent Firebase's array coercion is to ensure all your keys are strings to begin with. I usually prefix them with a fixed string, such as "id_99099". Commented Feb 22, 2017 at 5:14
  • It seems that "features" is an array of objects, and firebase is assigning each object an id. I want to be able to assign that object id. Forgive me if I'm not using the correct terminology. I would attach the full imported JSON file, but it's over 150mb... I was hoping this portion would show the issue well enough, as it's all structured the same way. Thanks again. Commented Feb 22, 2017 at 14:32

1 Answer 1

2

get rid of the square brackets and replace with squiggley brackets

this

{
    "flags": {
        "1": {
            "information": "blah",
        },
        "2": {
            "information": "It is great!",
        },
        "3": {
            "information": "Amazing!",
        }
    }
}

not this

[
    {
        "1": {
            "information": "blah",
        }
    },
    {
        "2": {
            "information": "It is great!",
        }
    },
    {
        "3": {
            "information": "Amazing!",
        }
    }
]
Sign up to request clarification or add additional context in comments.

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.