When referring to the JSON schema documentation, it becomes evident that your original string does not align with the JSON definition:
The initial string provided:
{0:'apple', 1:'bannana', 2:'guava'}
This is inaccurate for a few reasons:
Object keys should be strings (Numbers like 0
or 1
cannot be used, but strings like "0"
or "1"
are acceptable)
Strings need to be enclosed in double quotes "
A possible correction for the JSON representation of your data:
{"0":"apple", "1":"bannana", "2":"guava"}
// Consider enclosing this in different quotation marks for JavaScript
var json_d = `{"0":"apple", "1":"bannana", "2":"guava"}`;
// Alternatively, escape the double quotes
var json_d = "{\"0\":\"apple\", \"1\":\"bannana\", \"2\":\"guava\"}";
Or:
["apple", "bannana", "guava"]
// Wrap this in a different set of quotes for JavaScript
var json_d = `["apple", "bannana", "guava"]`;
// Or use escaped double quotes
var json_d = "[\"apple\", \"bannana\", \"guava\"]";
Tip: Utilize reputable JSON validators online, such as: