It is possible change form key in JSON from key in double quotes to key without quotes?
from
let a = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
to
let b = {
key1: "value1",
key2: "value2",
key3: "value3"
}
This is a valid JavaScript Object, but it is not valid JSON:
let b = {
key1: "value1",
key2: "value2",
key3: "value3"
}
If you are writing JavaScript, you can do it like that without issues.
You can also use single quotes or grave accents. These are both valid:
let b = {
key1: 'value1',
key2: 'value2',
key3: 'value3'
}
and
let b = {
key1: `value1`,
key2: `value2`,
key3: `value3`
}
In some situations, you need to include the single, double, or grave accents. For example, when your key starts with a number or has spaces in it:
You would need them for this:
let obj = {
'0key': 'value'
}
and this:
let obj = {
'john smith': 'value'
}
Here is some supplementary reading:
Consider dot notation while you are asking if it is valid.
b.key1 is valid dot notation, but b.0 is not, and neither is b.john smith.
The other notation is bracket notation which allows you to refer to keys that do not work with dot notation.
b['key1'] is valid bracket notation, and so is b['0'] and b['john smith'].