In JavaScript, a correctly formatted JSON string can be converted into an object. To qualify, it must be of correct shape, which is like this:
{
"property1": "value1",
"property2": "value2",
"property3": "value3",
}
As a string, it would look like this:
const json = "{ "property1": "value1", "property2": "value2", "property3": "value3" }"
Except, you cannot use quotes in this manner, or the JavaScript interpretter will get screwed up, so you have to escape them:
const json = "{ \"property1\": \"value1\", \"property2\": \"value2\", \"property3\": \"value3\" }"
This is what the utility JSON.stringify()
does. It turns it into a transport-safe shape. When you want to convert it back into an object, you use JSON.parse()
.
In JavaScript, you might do something like this:
const object = {
name: 'Bob',
age: 1337,
color: 'neon brown'
}
// I would like to send this to the Frontend now:
const preparedObject = JSON.stringify(object)
// Send it to the Frontend now
sendToFront({ payload: preparedObject })
// Lets pretend the Frontend does this:
const objectFromBackEnd = response.payload
// The Frontend gets it, but it is just a string it can't do anything with, so now we need:
const getObjectBack = JSON.parse(preparedObject)
if (getObjectBack.name === 'Bob') {
console.log('it worked')
}
// 'it worked'
In practice, you must parse
exactly as many times as you stringify
or JavsScript will not be able to convert the stringified object back to literal form.
I trust you can imagine how mangled it could get if you have double quotes inside double quotes inside double quotes. JavaScript can get equally confused.
To answer your questions specifically, the Backend (if it is using JavaScript) can and only does work with objects. The same is true for the Frontend, so the answer to your question 2 and 3 is both do, but you have to make sure they are interpretted correctly.
If you do JSON.stringify()
to a non-object, it will throw an error.
If you do JSON.parse()
to an improperly formatted JSON string, it will throw an error.
JSON is universally excellent because it provides a super friendly format for human-readers and super friendly for non-JavaScript machine-readers. It is both human and machine friendly, as compared to XML and other types of accomplishing the same thing.
Your missing link is you need to internalize how JSON.stringify()
and JSON.parse()
work. Test them out a bit and remove some "
, ,
, and :
characters and see how they respond.
Try this also:
const object = {
name: 'Bob',
age: 1337,
color: 'neon brown'
}
console.log('Test 1', JSON.stringify(object))
console.log('Test 2', JSON.stringify(JSON.stringify(object)))
console.log('Test 3', JSON.stringify(JSON.stringify(JSON.stringify(object))))
//Now try adding this after:
console.log(JSON.parse(JSON.parse(JSON.parse(JSON.stringify(JSON.stringify(JSON.stringify(object)))))))
This is strong for any programming language, because if your program outputs a valid JSON string, it doesn't matter how you created it. I just do JSON.parse('string')
from my JavaScript code and start reading it using dot notation and iterating through everything in the JSON "object".
You can read more here: http://www.json.org/