Parse nested JSON in .NET with similar data - c#

I have an API that I am calling that returns back a set of JSON. This one in particular is to return a set of Projects to me. These projects can have multiple sub-projects. The structure is the same for all of the sub-projects as it is the main projects. I can't seem to find anything that shows me how to parse the JSON so that it gives me some sort of usable class in .NET so I can show it on my web page.
Here is the structure (condensed because the original is HUGE and renamed projects and numbers to make it easier to read):
{
"data": [
{
"id": 70000,
"name": "Project 70000",
"children": [
{
"id": 71000,
"name": "Project 71000",
"children": [
{
"id": 71100,
"name": "Project 71100",
"children": [
{
"id": 71110,
"name": "Project 71110",
"children": [
{
"id": 71111,
"name": "Project 71111"
},
{
"id": 71112,
"name": "Project 71112"
}
]
}
]
}
]
}
]
},
{
"id": 80000,
"name": "Project 80000",
"children": [
{
"id": 81000,
"name": "Project 81000"
}
]
}
]
}
What I think I want my class to look like (I may be wrong here) is this:
public class data
{
public List<project> { get; set; }
}
public class project
{
public int id { get; set; }
public string name { get; set; }
public List<project> children { get; set; }
}
I am using JSON.NET but anything I've tried winds up not doing anything or leaves me with errors in which I cannot build the project. I've used JSON.NET in the past when there was a defined structure, but this one is throwing me for a loop!

You're very close. The issue seems to be that there is no property name for the list in your data class. I would name that property data (to match the property in the JSON), then rename the class to something meaningful that makes sense for what this data represents. Here I'm using ProjectTree for an example.
public class ProjectTree
{
public List<Project> data { get; set; }
}
public class Project
{
public int id { get; set; }
public string name { get; set; }
public List<Project> children { get; set; }
}
Then, deserialize to your ProjectTree class like this, and it should work fine:
var root = JsonConvert.DeserializeObject<ProjectTree>(json);
Fiddle: https://dotnetfiddle.net/5gmosp

You could create a self relationship for example:
public class Data
{
public List<Project> data { get; set; }
}
public class Project
{
public int id { get; set; }
public string name { get; set; }
public List<Project> children { get; set; }
}
var _data = JsonConvert.DeserializeObject<Data>(json);

Related

How to get partial properties from JSON returned from HttpClient fast?

I have a JSON snippet here taken from HttpClient class below in a C# .NET 5 program.
Simplified JSON:
{
"Restaurants":
[
{
"Id": 138898,
"Name": "Willesborough Cafe",
"Rating": {
"Count": 76,
"Average": 5.92,
"StarRating": 5.92
},
"CuisineTypes": [
{
"Id": 92,
"IsTopCuisine": false,
"Name": "Breakfast",
"SeoName": "breakfast"
}, {
"Id": 106,
"IsTopCuisine": true,
"Name": "British",
"SeoName": "british"
}
],
"Cuisines": [
{
"Name": "Breakfast",
"SeoName": "breakfast"
}, {
"Name": "British",
"SeoName": "british"
}
]
}
]
}
Current code:
dynamic result =
await _httpClient.GetFromJsonAsync<dynamic>(url);
// dynamic result2 = JsonConvert.DeserializeObject<dynamic>(result); // slow
dynamic result2 = JObject.Parse(result); // slow
I am interested to get the info from each restaurant below from the Restaurants array:
Name
Rating
CusineType
I use dynamic as I do not need to create multiple classes based on the JSON structure & I do not need to change my class if the JSON structure changes.
I have tried JsonConvert.DeserializeObject & JObject.Parse.
However, the Visual Studio debugging stuck at either of the method for a very long period
What is the recommended method to get partial properties from a huge JSON response?
Thanks
You can make a class with named properties
class Restaurant
{
public string Name { get; set; }
public Rating Rating { get; set; }
public List<CuisineType> CuisineTypes { get; set; }
}
class Rating
{
public int Count { get; set; }
public decimal Average { get; set; }
public decimal StarRating { get; set; }
}
class CuisineType
{
public int Id { get; set; }
public bool IsTopCuisine { get; set; }
public string Name { get; set; }
public string SeoName { get; set; }
}
and deserialize json to instance of Restaurant then you have a type you need. That's it.
You need to have a class contains list of Restaurant, because you must have a property equal name with your json object
class RestaurantList { public List<Restaurant> Restaurants {get; set;} }
Now you need a code to bind section of json to object
var restaurants = JsonConvert.DeserializeObject<RestaurantList>(result);

c# nested json to object (current with json.net)

i am new to c# and json.net.
I have a json configuration file and try to parse it to objects. But how can i map the relations correctly in the objects?
Currently the property in project loop is null.
And can the objects map "automatically" without mapping each property name/value?
I can also change the json!
configuration.json:
{
"debug": true,
"log": "database",
"projects": [
{
"name": "Name 1",
"showInfo": false,
"ranges": [
[
5,
6
],
[
9,
10
],
[
15,
20
]
],
"additional": [
{
"name": "subName 1",
"parameter": "ID"
},
{
"name": "subName 2",
"parameter": "ID2"
}
]
},
{
"name": "Name 2",
"showInfo": false,
"ranges": [
[
99,
100
]
]
},
{
"name": "Name 3",
"showInfo": false,
"ranges": [
[
44,
45
]
]
},
{
"name": "Name 4",
"showInfo": false,
"ranges": [
[
12,
14
]
],
"additional": [
{
"name": "subName xy",
"parameter": "ID"
}
]
}
]
}
my try to parse:
Configuration configuration = new Configuration();
JObject jObject = JObject.Parse(File.ReadAllText(filePath));
if (jObject.ContainsKey("debug"))
{
configuration.Debug = (bool) jObject["debug"];
}
if (jObject.ContainsKey("log"))
{
configuration.Log = (string) jObject["log"];
}
//loop projects
JToken projects = jObject["projects"];
foreach (JToken child in projects.Children())
{
var property = child as JProperty;
if (property != null)
{
var test = property.Name;
var test2 = property.Value;
}
}
the objects:
public class Configuration
{
public bool Debug { get; set; } = false;
public string Log { get; set; }
// this is propably wrong
public Dictionary<string, Dictionary<string, Project>> Projects { get; set; }
}
public class Project
{
public string name { get; set; }
public bool showInfo{ get; set; }
// wrong?
public int[,] ranges { get; set; }
// wrong?
public Additional[] Additional{ get; set; }
}
public class Additional
{
public string Name{ get; set; }
public string Parameter { get; set; }
}
You don't need a JObject at all json files can be desterilized to objects it's much more efficient because you don't create unneeded objects.
You Config class is just wrong
public class Configuration
{
public bool Debug { get; set; }
public string Log { get; set; }
public Project[] Projects { get; set; }
}
public class Project
{
public string Name { get; set; }
public bool ShowInfo { get; set; }
public int[][] Ranges { get; set; }
public Additional[] Additional { get; set; }
}
public class Additional
{
public string Name { get; set; }
public string Parameter { get; set; }
}`
Should look like this.
And then use. JsonConvert.DeserializeObject<Configuration>(json); To get the object.
If you have Visual studio it has this cool feature called paste special where you can just past your json and it will create a proper class for deserialization. It's under Edit-> Paste special-> Paste json as class

C# Web API Nested JSON from Stored Procedure in MSSQL

Overview:
I'm building an API using ASP.NET Web API 2. I'm building Stored Procedures in SQL and linking these in the API to serve data.
I know that SQL can return JSON using FOR JSON AUTO, for example. But, I don't think it's the best place to configure the data and out put JSON. So I'm assuming there must be a way to achieve nested JSON in the API.
This is what I want to achieve:
{
"Apps": [
{
"ItemID": "1",
"Path": "/AppReport",
"Name": "AppReport",
"Reports": [
{
"Reports_ItemID": "11",
"Reports_Path": "/AppReport/SubReport",
"Reports_Name": "SubReport"
},
{
"Reports_ItemID": "12",
"Reports_Path": "/AppReport/SubReport2",
"Reports_Name": "SubReport2"
}
]
},
{
"ItemID": "2",
"Path": "/AppReport2",
"Name": "AppReport2",
"Reports": [
{
"Reports_ItemID": "22",
"Reports_Path": "/AppReport/SubReport",
"Reports_Name": "SubReport"
}
]
}
]
}
At the moment it's coming out as a flat Array of Objects
{
"Apps": [
{
"ItemID": "1",
"Path": "/AppReport",
"Name": "AppReport",
"Reports_ItemID": "11",
"Reports_Path": "/AppReport/SubReport",
"Reports_Name": "SubReport"
},
{
"ItemID": "1",
"Path": "/AppReport",
"Name": "AppReport",
"Reports_ItemID": "12",
"Reports_Path": "/AppReport/SubReport2",
"Reports_Name": "SubReport2"
},
...
]
}
I have a class of the Stored Procedure in the API that looks like this:
namespace API.Stored_Procedures
{
public partial class SP_GetApps
{
public System.Guid ItemID { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public System.Guid Report_ItemID { get; set; }
public string Report_Path { get; set; }
public string Report_Name { get; set; }
}
}
My Controller looks like this:
[HttpGet]
[Route("{uid}", Name ="getReportApps")]
[ResponseType(typeof(SP_GetApps_Result))]
public IHttpActionResult SP_GetApps(string uid)
{
var res = db.Database.SqlQuery<SP_GetApps_Result>
("SP_GetApps {0}", uid);
return Ok(res);
}
In order to achieve the desired JSON Class should look like
public class App
{
public string ItemID { get; set; }
public string Path { get; set; }
public string Name { get; set; }
public List<Report> Reports { get; set; }
}
public class Report
{
public string Reports_ItemID { get; set; }
public string Reports_Path { get; set; }
public string Reports_Name { get; set; }
}

Patching an object with an array of objects

I have a many-to-many relationship between Courses and Students for a school. Here's is what I have for a Course:
public class Course
{
public Guid CourseId { get; set; }
public string Name { get; set; }
public ICollection<CourseStudent> Students { get; set; }
}
public class CourseStudent
{
public Guid CourseId { get; set; }
public Guid StudentId { get; set; }
}
I'm using JsonPatch to PATCH my objects. I'm trying to add to the collection of Students, just to the end of the collection with this:
[
{
"op": "add",
"path": "/Students/-",
"value": [
{
"CourseId": "07264DC9-9FEB-42E2-B1EF-08D58F58C873",
"StudentId": "FB6E6988-4A56-4CA4-86E2-E23090FAD98F"
}
]
}
]
But when I submit this, I get an exception saying:
"ClassName": "Microsoft.AspNetCore.JsonPatch.Exceptions.JsonPatchException",
"Message": "The value '[\r\n {\r\n \"CourseId\": \"07264DC9-9FEB-42E2-B1EF-08D58F58C873\",\r\n \"StudentId\": \"FB6E6988-4A56-4CA4-86E2-E23090FAD98F\"\r\n }\r\n]' is invalid for target location.",
The structure looks correct to me based on the Json Patch docs. Any idea why it won't accept my format?
Ended up figuring it out, the format should be:
[
{
"op": "add",
"path": "/Students/-",
"value":
{
"CourseId": "07264DC9-9FEB-42E2-B1EF-08D58F58C873",
"StudentId": "FB6E6988-4A56-4CA4-86E2-E23090FAD98F"
}
}
]

Update complex entity in db

I am working with facebook RTU and get json updates on the payments of my application`s users.
To have a better customer service i want to save all the json string facebook sends me , but in an ordered way.
For example,
I am getting this json:
{
"id": "3603105474213890",
"user": {
"name": "Daniel Schultz",
"id": "221159"
},
"application": {
"name": "Friend Smash",
"namespace": "friendsmashsample",
"id": "241431489326925"
},
"actions": [
{
"type": "charge",
"status": "completed",
"currency": "USD",
"amount": "0.99",
"time_created": "2013-03-22T21:18:54+0000",
"time_updated": "2013-03-22T21:18:55+0000"
}
],
"refundable_amount": {
"currency": "USD",
"amount": "0.99"
},
"items": [
{
"type": "IN_APP_PURCHASE",
"product": "http://www.friendsmash.com/og/friend_smash_bomb.html",
"quantity": 1
}
],
"country": "US",
"created_time": "2013-03-22T21:18:54+0000",
"payout_foreign_exchange_rate": 1,
"disputes": [
{
"user_comment": "I didn't receive my item! I want a refund, please!",
"time_created": "2013-03-24T18:21:02+0000",
"user_email": "email\u0040domain.com"
}
]
}
And i want to save it in db but not as a string, as an object this style and also have the option to query by field value from my db.
public class PaymentDetailsDto
{
public string id { get; set; }
public PaidUserDto user { get; set; }
public PaidApplicationDto application { get; set; }
public List<PaymentActionDto> actions { get; set; }
public RefundableAmountDto refundable_amount { get; set; }
public List<PaymentItemDto> items { get; set; }
public string country { get; set; }
public string created_time { get; set; }
public int test { get; set; }
public int payout_foreign_exchange_rate { get; set; }
public List<DisputeDto> disputes { get; set; }
}
How do you suggest representing this Entity in db?
As you see i have three List objects in my Root object and this it what make it a little but complicated to decide how to save it.
We are not gonna use EntityFramework or NHibernate.

Categories