Assuming you don't know which properties are JSON, you could use the replacer function parameter on JSON.stringify to check if a value is a JSON string. The below example tries to parse each string inside a try..catch , so is not the most efficient, but should do the trick (on nested properties as well)
var obj = {id:1, options:"{\"code\":3,\"type\":\"AES\"}"};
function checkVal(key,val){
if(typeof val === 'string'){
try{return JSON.parse(val);}catch(e){}
}
return val;
}
var res = JSON.stringify(obj,checkVal);
console.log('normal output', JSON.stringify(obj))
console.log('with replacer', res);
Answer from Me.Name on Stack OverflowJSON stringify objects with json strings already as values
.net core - How to stringify/normalize json in C# like JSON.stringify() would - Stack Overflow
TIP: How to Get Readable Format from JSON.stringify() - Tips & Tutorials - Keyboard Maestro Discourse
JSON Stringify for some fields
Videos
Assuming you don't know which properties are JSON, you could use the replacer function parameter on JSON.stringify to check if a value is a JSON string. The below example tries to parse each string inside a try..catch , so is not the most efficient, but should do the trick (on nested properties as well)
var obj = {id:1, options:"{\"code\":3,\"type\":\"AES\"}"};
function checkVal(key,val){
if(typeof val === 'string'){
try{return JSON.parse(val);}catch(e){}
}
return val;
}
var res = JSON.stringify(obj,checkVal);
console.log('normal output', JSON.stringify(obj))
console.log('with replacer', res);
No, you can't do that.
If you did not encode that string, JSON.parse will not return a correct string.
The cleanest solution to do that is use JSON for obj.options, and stringify it when you need to use it.
The problem is that you are comparing having an OBJECT in JS then convert it to JSON, to having a STRING in C# and then convert it to JSON.
If you had a C# object, the equivalent to JSON.stringify() would be just JsonConvert.SerializeObject(myObject). C# doesn't accept JSON syntax (like JS does) to define an object.
On the MDN samples you posted, you see:
console.log(JSON.stringify({ x: 5, y: 6 }));
The c# equivalent would be (run it):
Console.WriteLine(JsonConvert.SerializeObject(new { x = 5, y = 6 });
But that's just the way C# syntax works (Javascript allows JSON to define objects without having to parse them... C# has a different syntax for defining objects inline -anonymous or not-).
The equivalent in Javascript to the example you posted (having a string, and not an object) would be:
const jsString = '{"test": "test"}';
console.log(JSON.stringify(JSON.parse(jsString)));
Which is a bit different than just using JSON.stringify(), and matches what you are seeing in C# (deserializing then serializing)
Notice also that the syntax Javascript allows to define objects is not necessarily "strict valid JSON"... the above with this string would fail:
const jsString = '{ test: "test" }';
Whereas this way to define an object would be valid:
const jsObject = { test: "test" };
(that's, in fact, the reason you might want to "normalize" it as you call it)
All that said
if deserializing/serializing is a problem as in "looks", just make an extension method... something like:
public static string NormalizeJson(this string input) {
return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(input));
}
And then you can just do this on any string (if you have added the using on top):
myJsonInput.NormalizeJson();
See it in action
i think you missed formatting, it could be
public static string NormalizeJson(this string value)
{
return JsonConvert.SerializeObject(JsonConvert.DeserializeObject<object>(value), Newtonsoft.Json.Formatting.Indented);
}
» npm install fast-json-stringify