I generate object with EF and SQL to LINQ, and have complexs types which is returned from stored procedure. Now want to serialize those objects to nested JSON file, by using Newtonsoft json librarys.
The problem which have is that all serialized JSON files are flatten (cause returned result from procedures are all rows), now what want to ask is -
What tehnics to use to get nice structured (nested) JSON automatically (I need to serialize lot of procedure)?
Is there way to configure EF or SQL to LINQ to have functionality like polymorphic associations (like this, but that's old)
Example:
[{"key":value,"key":value,"key":value...}] --> generated JSON
Want get to look like:
{
"key": value,
"key": value,
.
.
.
},
"table1": <------ structured like this
{
"key": value
"key": value
},
"table2":
{
"key": value
"key": value
}
}
Related
I have a C# application that contains multiple classes with properties that correspond to entries in a Json file.
I would like to validate the Json file against a Json schema which I will create using NewtonSoft's schema generator for types e.g.
JSchema schema = generator.Generate(typeof(ClassName)).
My problem is that this will generate one Json schema per class and I want to validate the Json file against all of them.
How can I combine the Json schemas into one big super schema?
After generation of schemas, If you want the schemas to make one then you can use this oneOf and $ref methods.
{
"oneOf": [
{"$ref": "your path here"}, // the root schema, defining the object
{
"type": "array", // the schema.
"items": {"$ref": "your path here"}
}
]
}
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");
I am making a request to an API that returns information about a bunch of objects to me as a JSON string, similar to this: (handwritten so ignore possible typos)
{{
"count": 2,
"object": [{
"name": "object 1",
"fields": {
"Attribute1": 2,
"Attribute2": "string",
(...)}
"links": [{
"rel": "SomeString",
"url": "http://UrlString"
},
{
...
}]
}],
[{
"name": "object 2",
"fields": {
"Attribute1": 3,
"Attribute4": 5,
"Attribute6": "foo",
(...)
I turned this into a class by making a sample request and using "Insert JSON as class" in Visual Studio but, due to the fact that different Objects have different fields (API won't return a value when it's Null but I don't have a complete list of all possible fields), the class is already a 135 lines long and that was with a very basic request.
Also I am worried what might happen when I do happen to get a result that has a field that isn't specified in the class. For all I know it might cause an Exception or simply ignore everything that isn't explicitly specified.
Is there a way to work with these objects (I am trying to save them in an Azure SQL Server) without losing any information?
I was thinking about KeyValuePairs but I don't think that will work with var (because I don't know if the value is string or integer). Also it will make it awkward to write to SQL because every time I "discover" a new field the whole db would have to be re-created, but since Azure SQL Servers have JSON Support this might be easier than it sounds.
I would be perfectly happy with getting the "name" field, making it a field in the db and then storing the "fields" and the "links" field directly as a JSON string in the db but I need to access some of the fields during computation so I'd have to convert from Response to Object back to string. If I don't have every field in my class I am again worried that I'll lose something.
Sorry for the long text, hope I'm not seeing something here :)
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.
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