How to keep JSON keys as strings in JavaScript

Viewed 54

When running something such as the following:

https://onecompiler.com/javascript/3x4k9njds

The JSON keys will automatically change from strings to identifier names. I am writing to a Firebase Realtime Database, and Firebase will recognize the JSON as being invalid (since JSON keys need to have double quotes). How can I keep JSON keys in double-quotes (most JavaScript compilers will remove them)?

2 Answers

Console log don't show double quotes, because there's difference between JSON and JavaScript objects.

If you need to convert JavaScript Objects to JSON, you can use

JSON.stringify(newData)

Try

console.log(JSON.stringify({
  "msg": "hello world",
  "msg2": "testing123"
}));
Related