0

I am trying to add multiple records to the firebase database using cloud functions. To do this I am using a JSON array to hold JSON objects and converting it to a JSON string. I am running the below code in a loop where values of game_id,player1,player2 and card_obj changes.

    arr.push({
       game_id:{
           players:{   
               player1:player_obj1,
               player2:player_obj
           },
           game_type:card_obj
           }
       }); 
games_ref.update({active_games:JSON.stringify(arr)});

but I am getting output as:

"{\"19097718\":{\"playerObj\":{\"player1\":\"1o8CRZIfurbxnU5C5z6VoaAQMIP2\",\"player2\":\"8EhyHte506YzXYLbHDznVvQjKWl1\"},\"game_id\":\"26cards\"},\"42637418\":{\"playerObj\":{\"player1\":\"1o8CRZIfurbxnU5C5z6VoaAQMIP2\",\"player2\":\"8EhyHte506YzXYLbHDznVvQjKWl1\"},\"game_id\":\"13cards\"},\"52987418\":{\"playerObj\":{\"player1\":\"1o8CRZIfurbxnU5C5z6VoaAQMIP2\",\"player2\":\"8EhyHte506YzXYLbHDznVvQjKWl1\"},\"game_id\":\"13cards\"}}"
user8143570
  • 11
  • 1
  • 2
  • 1
    The only JSON in your question is the return value of `JSON.stringify(arr)`. Everything else is either an array (`arr`) or an object (in an object, in an object, in an object, ...) – Andreas Jun 22 '20 at 09:04
  • Also add what is the desired output you want, anyway if you want a plane string without escape characters you shouldn't be using json stringify I suppose... – pavan kumar Jun 22 '20 at 09:12
  • Don't stringify it, that's why its returned as a string. What is your expected result? If you must stringify it atleast parse it when its being outputted etc. – Sean Jun 22 '20 at 09:28
  • @Andreas ? Isn't an object literal just a JSON without the field names being in quotes? – Sean Jun 22 '20 at 09:31
  • @Sean No -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Jun 22 '20 at 09:45

0 Answers0