Is it possible to extract the JSON schema from a JSON string? - c#

I have a bunch of JSON strings, and would like to identify the schema of each one. This will allow me to keep a count of the different types of JSON objects in my collection of JSON strings.
I do not know the structure of these JSON strings ahead of time, and thus do not have a PONO to try to deserialize to.
I am currently using json.net, but am open to other options.

You could also use NJsonSchema for that,
see https://github.com/RSuter/NJsonSchema/wiki/SampleJsonSchemaGenerator

Previously JSchema was in JSON.NET Library but now its deprecated and they moved it to another library .. You Can Use Json.NET Schema ..It will easily Give You Schema of Any JSON String..even You Can Parse any JSON Using this Lib. ..like Suppose You Have JSON String ie _strJSON
JSchema schema = JSchema.Parse(_strJSON);

Related

Deserializing JSON string into string array

I'm somehow having troubles deserializing a json string into a simple List or string[] (I don't care which).
As of what I know, this is how to do this job:
JsonConvert.DeserializeObject<List<string>>(jsonString);
Here I am getting a RuntimeBinderException. It complains about the parameter, although my json string is valid and simple: a:1:{i:0;s:10:"Sahibinden";}
What you have isn't JSON is a serialized PHP object. There have been some tools that work well with this in C# but there isn't native support. If you own the PHP, then convert the object/array to JSON first. If not try the information on this question: https://stackoverflow.com/a/1923626/474702
Your JSON is invalid. Problems:
a:1 should be inside an object bracket of {}
The : before the { is invalid, you need a , there
The ; just after i:0 is invalid, you need a comma there
You repeat the mistake described in 1. and 2. inside your {} brackets as well
Solution: You need to read about JSON and make sure you understand its syntax.

Prettify JSON before storing to TXT

I'm trying to figure out how to prettify when the JavaScriptSerializer serializes my custom object to json.
string json = _jsonSerializer.Serialize(listToSerializeToJson);
it is just one long string right now, not formatted.
I don't see an option for that on the built-in JavaScriptSerializer class. However, if you're using Json.Net you can do this.
string json=JsonConvert.Serialize(listToSerializeToJson, Formatting.Indented);
According to Json.Net's project page, JavaScriptSerializer and DataContractJsonSerializer do not support creating indented prettified JSON strings.
If it's just for testing/human readability, I like using Json Lint: http://jsonlint.com/.

Embedd xml in json

I want to embed an xml string in a json string. I am returning this json from a web method and at client side I have to extract the xml string from this json data.
I tried this:
var data= $.parseJSON(jsonResponse);
But as the jsonResponse contains XML data it is becoming an invalid json and becomes unable to parse.
Is there any way to successfully embed xml string in json and extract it ?
EDIT:
Tried encoding xml string :
System.Security.SecurityElement.Escape(xmlString)
and then added it to json string.
Still at client side the json couldn't be parsed
EDIT
tried Ted Johnson's solution and the problem is partially fixed.
Now I could parse the json and extract the other attributes. But on accessing the xml attribute, it says undefined. Also couldn't decode it.
You will need to do the following.
Ensure the XML is encoded to project quote escaping. As the XML will need to be parsed as a string. In c# there is a standard way, URL Encoding using C#
ParseJSON
Access JSON attribute which has the xml encoded as a string and decode it. http://www.w3schools.com/jsref/jsref_decodeuri.asp
Parse the XML ... http://api.jquery.com/jQuery.parseXML/ and save result for use.

How to convert json from one format to another

I have a control that expects JSON in a particular format. I have a service that spits out JSON in a different format. What's the best way to convert the JSON to the expected format? C#, jQuery? I think I'd prefer to do it server-side.
With C# you can use System.Web.Script.Serialization.JavaScriptSerializer to parse your JSON string to an object or IDictionary.
MSDN: JavaScriptSerializer Class (System.Web.Script.Serialization)
With that object you could do anything you like, change values or maybe just parse it back to something like XML.

Generate different JSON from XML

I am trying to generate some JSON from an XML file, but not a straightforward conversion. I wish to pick and choose bits and have a slightly different structure.
I would rather not just concatenate a giant string together and was wondering if there were some decent libraries around to do this.
Also, for testing I would like to be able to validate the created json, just a simple check to see if it is valid JSON
Load the XML into a set of classes (use XMLSerializer) then implement JSON generator methods on those classes. Different methods, different JSON.
You can convert XML to other text representations pretty easily using XSLT, particularly file-to-file using xsltproc or a command-line version of xalan.
XSLT is sometimes an awkward programming language, but if you go this route, I have two recommendations for JSON conversions. Set your output to text, with a UTF-8 character set:
<xsl:output method="text" encoding="UTF-8" />
and run JSLint on the result, in order to catch any bugs in your XSLT file.
I would probably use Linq to XML (XElement and friends) to generate the new object and then pass that object to the Json serializer.
Other answers look good: I think I would also bind source format into objects, then serialize as the other formats. And any transformations would be done to objects, and not using data format representation. When using proper parser (for input) and generators/serializers (for output), you do not have to worry about well-formedness (resulting xml or json being syntactically correct).
And for biz-logic validity you could (and should) do it using objects.

Categories

Resources