I receive flattened json properties in the following format:
jsonName.propertyPath1.propertyPath2..., value
Example:
UniversityCollection.Persons.Person1.Name, Dave
I need to validate these kind of properties with a JSchema, that a receive from a file before generating a JObject with all the properties. I just have the program that collect them and generate the JObject.
It is important to note that i do not have knowledge about neither the structure of the Json nor the Json schema. I want to make a general program that validates any Json received through its associated Json schema.
Related
I have a scenario where I read the data from multiple json files and map them to certain Json format.
e.g. file1, file2, file 3
jsonoutput:
{
"parentfile1":"file1content",
"parentfile2":"file2content",
"parentfile3" : "file3content" and so on.
}
I am trying to map the input from the file reading directly to the valid json format to avoid reading all the files in one jsonObj, mapping it into one Object(class) and adding each parameter in the new object to create the jsonoutput.
I'd also like to handle shared mutable state while merging these json contents so I am using a lock(obj), the method that does this mapping is an async method.
I am using a JSchema to generate the schema of expected json format, :
check if the object in this schema contains the key(in the file), (not sure if this is possible??)
get it's parent name from the schema and add the parent name with the value(file content jsonobject) in the one JsonObject and
finally Deserialize the JSonObject in 2 to the expected file output.
Not sure if this is the best approach. Are there any other suggestion/ best practices to resolve this scenario?
I figured out one way to do this.
Generate the schema using JSchema
Get the properties of this schema -> Returns IDictionary with string as key-> property name and JSchema as value(child parameters/properties of the key)
Loop through the dictionary and get properties.value of the JSchema in 2
Loop through the List returned in 3-> List of keys
Add parent key as value and child key as key in the dictionary.
Now when adding the obj(file content) in the parent JObject, merge newJObject(parentkey, child object)
I have a API that I am sending requests to and receiving responses from, to start with I de-serialize a XML request template into a class object, which was just generated by using the paste XML as class option in Visual Studio, then amend the data in a few elements as needed, before serializing back into XML string and sending the request.
When I receive the response, I would also like to de-serialize this as there are a lot of repeated tags, so would be easier to get exact values out by doing:
var myvar = Root.Widgets.Widget1.WidgetPrices.PriceInPounds.value
The issue is if I get the XML response and paste it as a class object, I then have two classes with the same name because both the Request and Response share some but not all of the same structure, i.e both will contain Root.Widgets. And if I change any of the class object names, then the XML won't de-serialize properly into it. I'm not sure how to get around this so any suggestions appreciated.
I have some JSON here. The problem is it doesn't match the classes data types anymore. My question is; is it possible to deserialize JSON Dynamically? i.e. if I have entirely different JSON's can I deserialize them into two entirely different classes without first knowing what class I want to deserialize each into.
You can deserialize dynamic object with using newtonsoft
like bellowing code piece.
dynamic dynamicObj = JsonConvert.DeserializeObject(jsonStr);
string name = dynamicObj.data.code;
But in my personal preference is using strong type. I think its more convenience.
you can use quictype for generating c# classes from JSON object
quicktype generates strongly-typed models and serializers from JSON,
JSON Schema, and GraphQL queries, making it a breeze to work with JSON
type-safely in any programming language.
Hope the answer helps to you.
I have some JSON data :-
{
"mail":"mitch#domain.com",
"givenName":"User",
"sn":"Name",
"uid":"mitch",
"gecos":"User Name"
}
What I'm trying to do is de-serialize this into a List<KeyValuePair<string,string>>
I would normally do a dictionary, however some key's may be duplicated - this is the representation that is automatically generated by .NET when I pass a List<KeyValuePair<string,string>> object into the System.Web.Script.Serialization.JavaScriptSerializer class.
When I just plug the serialized object into the System.Web.Script.Serialization.JavaScriptDeserializer I get a empty response back.
From what I can see it should not be possible using the JavaScriptSerializer. The only way for customizing its behavior is by means of a JavaScriptConverter class, that will allow you to customize the serialization/deserialization process. Unfortunately both methods will pass an IDictionary for the properties, therefore the duplicated names are already merged. You might want to look into either a different format for your JSON or a different serialization library such as JSON.net which is way more customizable.
A Chat Room sample program created with NodeJs.
Node.js server will response 3 different format of Json.
I have created a Winform program to accept Json from Websocket.
I use Json.NET JsonMessage jsonResponse =
JsonConvert.DeserializeObject(e.Message.ToString());
to deSerialize one format of Node.js
How to identify different format of Json when serialized?
three type of Json Format
color : "{\"type\":\"color\",\"data\":\"blue\"}"
message : "{\"type\":\"message\",\"action\":\"Change Color\"}"
history : "{\"type\":\"history\",\"data\":[{\"time\":1384825833181,\"text\":\"this is test\",\"author\":\"Tom\",\"color\":\"green\"},{\"time\":1384842730192,\"text\":\"WinForm Send\",\"author\":\"WinForm Say Hello!\",\"color\":\"orange\"},{\"time\":1384842808185,\"text\":\"WinForm Send Again!!!\",\"author\":\"WinForm Say Hello!\",\"color\":\"red\"},{\"time\":1384843229766,\"text\":\"I am fine\",\"author\":\"who are you\",\"color\":\"red\"}]}"
All of the 3 Json formats can create 3 different Class with JsonProperty to map them.
I can verify string with the first few characters.
Are there any other solutions?
I found the following solution could help.
Use JsonCreationConverter . How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?
Use JavaScriptSerializer with dynamic type
Parse json string using JSON.NET
var jss = new JavaScriptSerializer();
dynamic data = jss.Deserialize<dynamic>(e.Message.ToString());
Use JObject.Parse with dynamic type
Deserialize json object into dynamic object using Json.net
Serialized data is as string so, its just a string. As you have also told that you want to identify JSON format. So, better is first convert to JSON & then identify the JSON data type and based on the type call the process or method to process that data.