2

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 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I am aware. I think the Invalid JSON error must have been for something else because the issue resolved after a while, despite me still using a JavaScript object.
0

Try

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

1 Comment

Firebase Realtime Database requires input as a JSON object. In my program, I will get the permission denied response. While trying the same thing in the Rules Sandbox, I get the error "Invalid JSON". I believe the JSON is invalid simply because JavaScript automatically changes the keys and messes up the syntax. ibb.co/ydD6FFc

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.