How to dynamically parse only part of the JSON - c#

I have the next issue: I receive a lot of JSONs in different formats. I need to process only part of these documents. I tried to use Newtonsoft.Json.Schema nuget, but got the next problem:
JSON how to don't parse additional properties
Can you suggest me some ways to parse only part of the JSON document, when we don't know structure of this json? We can store some schema of the document.
Example:
We have the next JSON document.
And here we need to process, for example, only name and age properties. And I will know these properties only in runtime.
{
'name': 'James',
'age': 29,
'salary': 9000.01,
'jobTitle': 'Junior Vice President'
}

If your json does not contain nested fields, so each of the top level fields are primitive types (not objects) then you can deserialize it as Dictionary<string, object> like this:
var fieldValueMap = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
string fieldName = "name";
Console.WriteLine(fieldValueMap.ContainsKey(fieldName)
? fieldValueMap[fieldName]
: "There is no such field");

Related

Dynamic loading of JSON file, and all of its contents with C#

Recently I've gotten into JSON parsing, and I was wondering, is it at all possible to completely dynamically load all of the contents within a JSON file? And by dynamically load, I mean, load all values of a JSON file without knowing any keys, meaning I cannot do the following code:
string contents = File.ReadAllText("SomeJsonFile.txt");
JObject obj = JObject.Parse(contents);
var value = obj["SomeKey"];
The reason I cannot do the code above, is because that would require the application to know the key ahead of time.
My goal is to be able to load any JSON file, and retrieve all values from said JSON file. I want to be able to load nested values, along with root values, all without knowing the keys.
Is there a way for me to do this? If you need any more information, please don't hesitate to ask.
The way I want to use this data is to first, bind a textbox to a string version of each key. Then I will dynamically add the TextBoxes to a FlowLayoutPanel, I also want the user to be able to change this data, and the JSON to change with it.
Please help.
If you don't know what the keys you are going to have you can use JArray from Json.NET to dynamically access the class.
See example instantiation and usage in the answer here:
How to access elements of a JArray (or iterate over them)
Assuming the JSON is an object and not an array, you could deserialize the json string to a IDictionary<string, object> using Json.NET
string myJsonString = File.ReadAllText("SomeJsonFile.txt");
var dataObj = JsonConvert.DeserializeObject<IDictionary<string, object>>(myJsonString);
var topLevelKeys = dataObj.Keys;
Your original question already shows you've successfully parsed a JSON string into a C# object, without knowing the keys.
It appears your actual question is on how to flatten the JObject into some kind of enumerable set of key/value pairs. For that, I refer you to the answers on this thread:
Generically Flatten Json using c#
Answer 2 IMO looks a lot cleaner:
string contents = File.ReadAllText("SomeJsonFile.txt");
var schemaObject = JObject.Parse(contents);
var values = schemaObject
.SelectTokens("$..*")
.Where(t => !t.HasValues)
.ToDictionary(t => t.Path, t => t.ToString());

Validate YAML against a Schema in C#

I have some YAML files, and I want to define a schema and then validate the YAML against the schema before trying to work with it in C#.
From my research there doesn't appear to be a schema language specific for YAML, but people use JSON Schema to validate YAML files. So that's what I tried.
The Yaml library I'm using (YamlDotNet) doesn't appear to be able to do schema validation, so I tried to do what I saw in some other examples. Convert the YAML to JSON then use Newtonsoft.Json to validate against my schema.
var schemaJson = File.ReadAllText(schemaPath);
var schema = Newtonsoft.Json.Schema.JSchema.Load(new JsonTextReader(new StringReader(schemaJson)));
var deserializer = new YamlDotNet.Serialization.Deserializer();
var data = deserializer.Deserialize(new StringReader(yaml));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
var serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Serialize(sw, data);
Newtonsoft.Json.Linq.JObject.Parse(sb.ToString()).Validate(schema);
The problem with this is when I deserialize the YAML, it stuffs the data into a Dictionary, and ultimately every value is a string. So when I serialize back to JSON all the values get wrapped in single quotes, and when I try to validate against my schema, anything that the schema says is an integer/number will fail.
e.g. This YAML:
name: John Doe
age: 27
will get converted to JSON something like this:
{
name: 'John Doe'
age: '27'
}
And when I try to validate against my JSON Schema that says age should be an integer that fails.
Yes, you can use JSON Schemas to validate data from YAML files, as the data model that YAML is uses is very close (but not identical) to JSON. And most JSON Schema evaluators tend to work with deserialized data anyway, after it has been decoded from JSON or any other data format that uses a similar data model.
Your problem sounds like a misconfiguration in your YAML deserializer. The YAML format is capable of differentiating strings from numbers, so the serializer should do so as well, or at least offer different options for handling strings that look like numbers.

Getting data from service however object is empty

var details= _clientService.GetAsync<DoctorDetails>(getDetails).Result;
I get the Result from the service which is JSON when I use "object" in the GetAsync instead of DoctorDetails. However, I don't see any property values being filled in details (All are null in DoctorDetails). DoctorDetails is the cs file of the schema I generated through xsd.
DoctorDetails is an auto generated file that contains properties like
Name
ID etc
How to deserialize this and get values in those properties (in the details variable above)
Edit
It is only returning values if I make the syntax like this
var details= _clientService.GetAsync<object>(getDetails).Result;
If you haven't already tried this option, use a library called Json.Net from Newtonsoft for json stuff. Newtonsoft json.
Provided you have the schema details and the property names match you may try the following..
var details= _clientService.GetAsync<object>(getDetails).Result;//Please check if this is a string else use .ToString()
/*
"{
'Name': 'Doctor Who',
'ID' : '1001'
}";
*/
DoctorDetails m = JsonConvert.DeserializeObject<DoctorDetails>(details);
Documentation Deserialize an Object.
I'm not promoting this library, it's just a suggestion. :)
Hope it helps.

How to dynamically get items from Json feed

I need to pull and display some json data from a google stock feed:
https://finance.google.com/finance/info?client=ig&q=NYSE:BHP
The only catch is I don't know what the data is. The customer would like to pass in a list of comma separated values to tell my code what items to pull.I would therefore plan to pass in an array of named items to tell the feed which items I want values for, so in theory to match the incoming values with the equivalent json item names. It wont always be the same items or number of items.
How can I do this dynamically (I am using json.net) ?
Sample Json Data:
[{
"id": "4905",
"t": "BHP",
"e": "NYSE",
"l": "26.90",
"l_fix": "26.90",
"l_cur": "26.90",
"s": "0",
"ltt": "6:01PM EST",
"lt": "Dec 2, 6:01PM EST",
"lt_dts": "2015-12-02T18:01:42Z",
"c": "-0.41",
"c_fix": "-0.41",
"cp": "-1.50",
"cp_fix": "-1.50",
"ccol": "chr",
"pcls_fix": "27.31"
}]
Sample of CSV values the user might pass in:
t, e, l, cp_fix
You can use the json class from System.Web.Helpers namespace to deserialize json string into a dynamic object. like below:
dynamic Data = Json.Decode(json);
It is included with the MVC framework as an additional download to the .NET 4 framework. Than you can access the properties you want using Data.PropertyName

How to get json output from a webservice as a datatable in c#?

i have a webservice (wsdl) generated from a java project. its output is getting as json format.
something like this
[{"pid":163686,"chartno":null,"lName":"Bec&&kwith","fName":"Burt","mName":null,"line1":"312 HILL ROAD","line2":null,"city":"Hillsboro","state":"Missouri","pinCode":null,"phone":"123456879","dob":"1947-01-01","gender":"Male","ssn":null,"martialStatus":null,"guarantor":null,"sig":null,"priInsname":null,"priPolNo":null,"secInsname":null,"secPolNo":null}]
i want to use this data in my c# web application to display it in a grid.. how can i get this data into a datatable?
how to format the json output to a table structure if multiple data comes?
The heading will be pid, chartno, ... etc .
i dont want to make it too complex... i just want the json output to store into a dictnory or somthing ..
thank u
I would deserialize the JSON in to a custom c# object instead of a DataTable.
Deserialize JSON string to c# object
Essentially you can use the JavaScript serializer to deserialize your JSON string into the object. I recommend this over the data table since you would have to manually map it to the table The grid should be able to consume a List of your custom object instead of a data table
You need to Parse json , you can JQuery to parse json object.

Categories