I am creating one json array using Json-glib using c++. Here is the data:

title="Daniweb"         (string)
last= 1234              (long)
icon="icons"            (string)
url = "[\"dani.com\"]"   (json array)

After making one json array using exsiting APIs of json-glib library, I got this:

{"id":"Nitin1","clientName":"Gourav_first_task","tabs":"[{\"title\":\"Daniweb\",\"icon\":\"icons\",\"urlHistory\":\"[\\\"Dani.com\\\"]\",\"lastUsed\":1234}]"}

Is it the correct json string made? It has added those escape characters to it. I have declared those variables like that only which I have written above and got json as given. Please tell is it the correct json made out of it? Thanks.

Recommended Answers

All 3 Replies

I know there is validator. But I want to know it is the correct json that should be made from that data. I am confused about those slashes. How they are added by Json and how they are removed.Please explain little bit.

Slashes are marking escape characters. When you read a JSON message, you must interpret certain characters as separators. For example, if you have "some phrase", it is seen as the string 'some phrase' (no quotes, because the quotes delimit the string, they are not part of it). What if you want to say "a double-quote " in a string is confusing". This will confuse the JSON interpreter because it cannot know that the double-quote in the middle of the string is part of the string, it will think instead that it marks the end of it. To solve the issue, escape sequences are used to resolve the ambiguity. If you have \", then it means that it is a literal double-quote, not a delimiter for the end of a string. Similarly, if you have \\, it means that it is a literal slash, not the start of an escape sequence. There are several other escape sequence like that.

The complication with things like JSON is that nearly everything needs to be escaped because there are lots of meaningful delimiters and there are nesting of messages. Part of the art of generating and parsing JSON messages is knowing when to escape and how to interpret escaped and non-escaped special characters. AFAIK, XML formats are easier for that because they don't have nested messages.

We can take your message to see what I mean by complications. Starting with this:

{"id":"Nitin1","clientName":"Gourav_first_task","tabs":"[{\"title\":\"Daniweb\",\"icon\":\"icons\",\"urlHistory\":\"[\\\"Dani.com\\\"]\",\"lastUsed\":1234}]"}

we see that the "tabs" value is a string:

[{\"title\":\"Daniweb\",\"icon\":\"icons\",\"urlHistory\":\"[\\\"Dani.com\\\"]\",\"lastUsed\":1234}]

and so, the next step is to make a pass to resolve all the escaped characters, we get:

[{"title":"Daniweb","icon":"icons","urlHistory":"[\"Dani.com\"]","lastUsed":1234}]

and in there, we have the string [\"Dani.com\"] (where the \" come from the escape sequences \\\". By peeling off that final layer of escape sequence, we get ["Dani.com"].

Just imagine, if you have 5 layers of nested messages, you can easily end up with something like:

\"
\\\"
\\\\\\\"
\\\\\\\\\\\\\\\"
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"

That's one of the aberrations of text-based formats and escape sequences. And this is the reason why text-based formats should not use nesting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.