JSON Object With or Without Quotes

I am trying to learn JSON, i learned that any javascript object with the key in double quotes are considered as JSON object.

And i constructed this object

var jstr1 = {"mykey": "my value"};

But when i try to parse using JSON.parse(jstr1), i got the following error. see the screenshot.

enter image description here

But when i try to parse this

var jstr = '{"mykey": "my value"}';,

i got the success, see the screenshot. i got confused with this. Please explain me why this happens. what is the difference between the two forms.

And when i got JSON as a response from any services, how it would look like, whether it will be in form of jstr or jstr1

thanks in advance for any help.

Answers:

Answer

You are creating a Javascript Object. If you want a JSON-string from it, use JSON.stringify.

So

var myObj = {mykey: "my value"}
   ,myObjJSON = JSON.stringify(myObj);

Based on comments: There is no such thing as a JSON Object. There are JSON-strings, which can be parsed to Javascript Objects. Javascript Objects can be stringified to JSON strings. Within a JSON-string keys and values are quoted. So the result of the above is a string containing '{"mykey":"my value"}'.

Try parsing myObjJSON in your browser console (using: JSON.parse(myObjJSON)) and you get: Object {mykey: "my value"}.

Answer

This code

var jstr1 = {"mykey": "my value"};

creates a JavaScript object using the Object Literal Notation.
For the difference between the Object Literal Notation and JSON (JON is short for JavaScript object notation), see here: What is the difference between JSON and Object Literal Notation?

It makes logically no sense to pass this data to JSON.parse().

The difference to your first variant (var jstr = '{"mykey": "my value"}';) is that it creates a 'raw' string. You cannot access anything on that string except the raw character sequences. Using JSON.parse() gives us a usable form (object) created from the string.

SyntaxError: Unexpected token o

This comes from the automatic string conversion of jstr1:

jstr1.toString();
// gives us [object Object]
// ----------?
Answer

You have some missunnderstanting for JSON.parse

JSON.parse takes string and parse it to JAVASCRIPT object
JSON.stringify takes an object and parse it to a string

thats why when you ran the following

JSON.parse('{"a":"b"}')

it worked because it expects a json string

but when you ran

JSON.parse({"a":"b"})

it didnt because the object was coverted to string which is

"[object Object]"

and here is the error where "[object Object]" is not valid syntax at letter o

Answer

JSON.parse() accepts a string and converts to JSON object, it doesnt take a javascript object as the parameter. Refer JSON.parse() It could give you the results as follows

JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null

and do know that If the string to parse is not valid JSON, a SyntaxError exception is thrown. so this is how you get syntax error on jstr1 (It is not a JSON string)

Answer

How about this:

MarahJSONObject gtp = new MarahJSONObject()
    gtp.put("ecomm_prodid", "123")
    gtp.put("ecomm_pagetype", "cart")
    gtp.put("ecomm_totalvalue", "19.99")

String r = gtp.toString()
    gtp.keySet().each {
        r = r.replace(/"${it}"/, it)
    }
    println r

then you will get: {ecomm_pagetype:"cart",ecomm_prodid:"123",ecomm_totalvalue:"19.99"}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.