-1

I exported the database from Firebase in Json format, and It looks weird but it's legit JSON syntax. Does anyone know how to parse this into a list of objects?

JSON code:

{
  "users" : 
  {
    "GSIgfyiEGtZs5reYe4SpwFJVxDC2" :
    {
      "email" : "anic@hotmail.com",
      "password" : "Dava123",
      "score" : 0,
      "username" : "Anic2",
      "zdate" : "2-3"
    },
    "OHxA5ARnbYdsy9Ga1nxDy0gZQBv1" : {
      "email" : "dava@hotmail.com",
      "password" : "Dava123",
      "score" : 3,
      "username" : "dava",
      "zdate" : "01-03-2021  11:53"
    }
  }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Davasoft
  • 1
  • 1
  • 1
    `users` is not a JSON array, it's a JSON object with an arbitrary number of runtime property names with a fixed schema for the values. Since you don't know the properties of the `users` object in advance, maybe see [Javascript: Iterating over JSON objects](https://stackoverflow.com/q/42352161/3744182). – dbc Apr 02 '21 at 16:55

1 Answers1

0

If you are trying to access the values, you can use the keys attribute

var myFireBaseObj = {
  "users" : 
  {
    "GSIgfyiEGtZs5reYe4SpwFJVxDC2" :
    {
      "email" : "anic@hotmail.com",
      "password" : "Dava123",
      "score" : 0,
      "username" : "Anic2",
      "zdate" : "2-3"
    },
    "OHxA5ARnbYdsy9Ga1nxDy0gZQBv1" : {
      "email" : "dava@hotmail.com",
      "password" : "Dava123",
      "score" : 3,
      "username" : "dava",
      "zdate" : "01-03-2021  11:53"
    }
  }
};

console.log( Object.keys(myFireBaseObj));

// if you want to get both values and keys / users etc seperately as objects
var keys = Object.keys(myFireBaseObj );
var values = Object.values(myFireBaseObj );

console.log(keys[0]);
// put your attribute you want here
console.log(values [0]['...']); 

Option 2: if you want an array back, use map for e.g. adapt it below

 Object.keys(myFireBaseObj).map(function(a,i){
 // whatever you want in the array you map it here
 // you have to customize the nested levels
 return
 [i,myFireBaseObj[a].blahBlah.. ,
 myFireBaseObj[a].password] 
 })

Update: Added sample to show for loop to get the key, values

for (var key of Object.keys(myFireBaseObj)) {
    // this will give you the key & values for all properties
    console.log(key + " -> " + p[key])
    // .. process the data here! 
}
Transformer
  • 6,963
  • 2
  • 26
  • 52