-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" : "[email protected]",
      "password" : "Dava123",
      "score" : 0,
      "username" : "Anic2",
      "zdate" : "2-3"
    },
    "OHxA5ARnbYdsy9Ga1nxDy0gZQBv1" : {
      "email" : "[email protected]",
      "password" : "Dava123",
      "score" : 3,
      "username" : "dava",
      "zdate" : "01-03-2021  11:53"
    }
  }
}
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. Commented Apr 2, 2021 at 16:55

1 Answer 1

0

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

var myFireBaseObj = {
  "users" : 
  {
    "GSIgfyiEGtZs5reYe4SpwFJVxDC2" :
    {
      "email" : "[email protected]",
      "password" : "Dava123",
      "score" : 0,
      "username" : "Anic2",
      "zdate" : "2-3"
    },
    "OHxA5ARnbYdsy9Ga1nxDy0gZQBv1" : {
      "email" : "[email protected]",
      "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! 
}
Sign up to request clarification or add additional context in comments.

2 Comments

How can I access to second user's property password?
create a for loop.. I added a sample above.

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.