Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing JSON string via command line

Tags:

json

c#

cmd

I'm serialize an object to json string and passing that to an app through command line but when I receive the arguments in the app, this is no longer a json string but a regular string .

I'm using below code to create json string

var jsonStr = new JavaScriptSerializer().Serialize(obj);

string that I'm passing - "{"name":"abc","place":"xyz"}"
string that I receive - "{name:abc,place:xyz}";

How can I maintain the structure of the json string ?

like image 872
Kapil Avatar asked Mar 09 '15 10:03

Kapil


People also ask

Can I pass JSON as string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

How pass JSON data in Curl Post?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.

How do I convert a string to JSON?

String data can be easily converted to JSON using the stringify() function, and also it can be done using eval() , which accepts the JavaScript expression that you will learn about in this guide.


2 Answers

I guess double quots are wiped out because they've meaning inside the CLI world.

I would say that converting the whole JSON into a base 64 string, and then, in the CLI internally turn into regular string again should work:

var jsonStr = Convert.ToBase64String(Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(obj)));

// When your receive the whole string....
var jsonStr = Encoding.UTF8.GetString(Convert.FromBase64String(inputStr));

// Now deserialize your JSON string into a regular .NET object...

Answering to some comment from @Panagiotis Kanavos...

The CLI doesn't corrupt any string - people would have noticed by now. If the OP sends a different string, the problem is in that code, not the CLI

As I pointed out in comments as answers to your ones, I know that inside .NET world we don't need to escape a JSON and maybe in other environments there's also no need to do this.

I suggested base 64 approach because it's a bullet-proof solution, and as OP provided few details and insufficient code samples, at the end of the day, I believe it's the base 64 approach is as valid as just escaping double quots, but it also provides an added value: it escapes any special character in addition to ".

BTW, there're actual cases where a CLI prevents some characters. redis-cli on Windows doesn't allow curly brackets...

Update 2

Since @Panagiotis Kanavos has confused CLI with Common Language Interface, I want to be sure that everyone that reads my answer understand CLI as Command Line Interface.

like image 101
Matías Fidemraizer Avatar answered Oct 07 '22 05:10

Matías Fidemraizer


Just escape quotes with backslashes so CMD will not remove quotes inside a JSON.

String which should be passed:

"{\"name\":\"abc\",\"place\":\"xyz\"}"

String which will be received:

{"name":"abc","place":"xyz"}

Use this code to escape the string:

string jsonString = new JavaScriptSerializer().Serialize(obj);
string escapedString = jsonString.Replace("\"", "\\\"");
like image 7
Yoh Deadfall Avatar answered Oct 07 '22 05:10

Yoh Deadfall