How to send a json string in a json request? - json

I need to send a json string in the json request and pass it on the backend to be unmarshalled as an object. ie a JSon request has a string attribute which can be converted to an object later. What's the format for that?
I tried {"name":"bob","address":{"state":"ny","city":"newport"}}. I dont want "address" : {...} to be unmarshalled here.. i want it to be sent as it is to backend as a String.
FWIW, i m using jackson , and I dont think that should matter.
Also, I dont want to escape the characters ie
"address":{\"state\":\"ny\",\"city\":\"newport\"}

I wouldn't try to manually encode json yourself, Use an appropriate Json encoder for whatever language you're using.
{"name":"bob","address":{"state":"ny","city":"newport"}} is not valid json. the entire string should be quoted. like:
"{\"name\":\"bob\",\"address\":{\"state\":\"ny\",\"city\":\"newport\"}}"
and it will arrive at the backend as a string. thats what a json string is, a string.
you need to decode it server side with an appropriate json decoder
if you dont want to escape the quotes then use a combination of single and double quotes like:
'{"name":"bob","address":{"state":"ny","city":"newport"}}'

Using php:
<?php
$a = json_encode(array("state"=>"NY", "city"=> "Newport"));
$b = array("name"=>bob, "address"=>$a);
print_r($a);
print "\n";
print_r(json_encode($b));
print "\n";
?>
Output
{"state":"NY","city":"Newport"}
{"name":"bob","address":"{\"state\":\"NY\",\"city\":\"Newport\"}"}
The last line is your answer - but making the code do it for you is always preferable.

Related

Swift Json Single Quote Parse

I have a problem while parsing json which includes single quote. I am using JSONDecoder. I added response from API at below and I don't want to do any replacement or some regex operations. Are there any workaround for that?
"{\'value1\': true, \'value2\':\'2021-02-08\'}"
Your string is simply not valid JSON. Since it's not valid JSON there's no way to configure JSONDecoder to decode it.
If you aren't in charge of the service that outputs that string, the only thing you can do to use it with JSONDecoder is to modify the string to become valid JSON by, as you suggested, doing text replacement.

Json : getting the unwanted result

I'm using json plugin to get the response in json.
But I m getting the unwanted result:
Here is what I get:
{"data":"[[\"service\",\"webservices\",\"document\"],[\"validation\",\"adapters\",\"server\"]]","records":25,"recordsTotal":75}
originally the data var in my action class is like this:
[["service","webservices","document"],["validation","adapters","server"]]
but json plugin adds the backslash.
The wanted result is that:
{"data":[["service","webservices","document"],["validation","adapters","server"]],"records":25,"recordsTotal":75}
Is there a way to get the later result ?
Thanks
You're representing the data as a PHP string. " is obviously a reserved character in JSON, so your serialization library is dutifully escaping the quote using /.
If you set up the PHP variable so it's an array of arrays, instead of a string representing an array of arrays, then your JSON serialization will work fine.

Why to use NSData to Json

Every time I make a call to Json, I need to use NSData and Json.deserialize.
Since Json is just a string, why cannot I just read it through rest and then use it at the other side, without using NSData, Json.Serialization, etc ?
JSON is a method for sending data through strings (not binary). Using as a string is possible, but you'd have to parse it yourself:
Here's a JSON string:
"{\"test\": \"foo\"}"
and it can be converted to
{test: "foo"}
and used like a Hash/Dictionary.
I mean, you can't get the use a string as a dictionary, can you?
"Nope.".get("Nope")
JSON is not a string. JSON is a binary format. That binary format is human readable but it's not a string. That's why we have to use NSData and why we have tools for JSON encoding and decoding instead of creating JSON data by appending strings together.

escape special characters inside json from a data attribute

I have json stored in data attributes.
<div data-dataarray="[["Shipper","Ship No","Weight"],["1WWWQUICK\PARTSCOM",1,1]]">
data = $('#'+moduleId).data('dataarray')
So data is now a string.
Which I then need to parse to get it back to json:
jsondata = JSON.parse(data);
This json can have special characters (notice the backslash)... which causes an error. How can I escape them before/while parsing?
firstly
I think the html5 data attributes need to have a form like data-xyzUserVariable. then you retrieve them using jquery.data("#xyz_id", "xyzUserVariable"),
secondly
However, be wary that jQuery cleverly attempts to convert the data to a suitable type (booleans, numbers, objects, arrays or null) and avoids touching the DOM.
thirdly
your json seems to be an array of objects ..is it missing an ending bracket ']' ?

Does HTML need to be encoded when passed back in JSON?

When passing HTML back through a response in JSON format, does it need to get encoded?
Yes. You would pass the HTML code in a string, so any quotation marks and backslashes in the code would have to be encoded.
Example:
<div onclick="alert('Line 1\nLine2');">show</div>
would be encoded into a string like this:
"<div onclick=\"alert('Line 1\\nLine2');\">show</div>"
and for example put in a JSON object representation like this:
{"html":"<div onclick=\"alert('Line 1\\nLine2');\">show</div>"}
Simple answer "no" JSON does not need to be encoded when passed back in JSON. JSON object should be DIRECTLY parsable by a javascript engine. Check the following:
JSON Standard
JSON Lint