C# Newtonsoft JSON map property to array sub item - c#

I'm using Newtonsoft to deserialize JSON data to an object.
My JSON looks like this:
{
"id": "4aa50d01-41bd-45e3-803e-f479a948acf1",
"referenceNumber": "120064",
"status": "Application in Progress",
"borrowers": [
{
"name": "John Doe",
"type": "BORROWER"
},
{
"name": "Jane Doe",
"type": "COBORROWER"
}
],
"propertyAddress": {
"zipCodePlusFour": ""
}
}
The borrowers array can have up to 2 items. 1 with type == "BORROWER"and the other with type == "COBORROWER"
I have a LoanItem class I am deserializing to.
public class LoanItem
{
public string referenceNumber { get; set; }
public string status { get; set; }
}
I know I can mark the LoanItem property with the JSONProperty attribute but I'm wondering if there is a way I can add an array sub item with a condition.
Something maybe like
[JSONProperty("borrowers[WHERE type = 'BORROWER'].name")]
public string BorrowerName { get; set; }
[JSONProperty("borrowers[WHERE type = 'COBORROWER'].name")]
public string CoBorrowerName { get; set; }
Is this possible? Can I use the JSONProperty attribute?

Create a new class Borrower
public class Borrower
{
string Name { get; set; }
string Type { get; set; }
}
Update your LoanItem class to this
public class LoanItem
{
public string referenceNumber { get; set; }
public string status { get; set; }
public List<Borrower> Borrowers {get;set;}
public string BorrowerName { get { return Borrowers.Where(x=>x.Type == "BORROWER").FirstOrDefault().Name; }
public string CoBorrowerName { get { return return Borrowers.Where(x=>x.Type == "COBORROWER").FirstOrDefault().Name; } }
}
Now you can access the BorrowerName and CoborrowerName

Related

Deserialize Json string with child objects

Got the following structure given:
public class TaskList
{
public string Name { get; set; }
public List<ToDoTask> ToDoTasks { get; set; }
}
public class ToDoTask
{
public string Name { get; set; }
public string Note { get; set; }
public DateTime LastEdit { get; set; }
public bool Finished { get; set; }
}
I'm using System.Text.Json in .NET 5.0 to serialize a List successfully into a json-file:
JsonSerializerOptions serializeOptions = new() { WriteIndented = true };
string json = JsonSerializer.Serialize(taskLists, serializeOptions);
the result looks fine:
{
"TaskLists": [
{
"Name": "List1",
"ToDoTasks": [
{
"Name": "Task1",
"Note": "",
"LastEdit": "2022-04-19T13:05:10.0415588+02:00",
"Finished": false
},
{
"Name": "Task2",
"Note": "",
"LastEdit": "2022-04-19T13:05:13.9269202+02:00",
"Finished": false
}
]
},
{
"Name": "List2",
"ToDoTasks": [
{
"Name": "Task3",
"Note": "",
"LastEdit": "2022-04-19T13:05:18.3989081+02:00",
"Finished": false
},
{
"Name": "Task4",
"Note": "",
"LastEdit": "2022-04-19T13:05:23.0949034+02:00",
"Finished": false
}
]
}
]
}
When I deserialize this json-file, I only got the TaskLists but the ToDoTasks, are empty.
List<TaskList> taskLists = JsonSerializer.Deserialize<List<TaskList>>(json);
What do I have to do, get also the ToDoTask-Childs included into the deserialized objects?
Whenever you cannot figure out your model class, you can use Visual Studio's Edit - Paste Special - Paste JSON as Class to check out.
Your model classes should be like this:
public class Rootobject
{
public Tasklist[] TaskLists { get; set; }
}
public class Tasklist
{
public string Name { get; set; }
public Todotask[] ToDoTasks { get; set; }
}
public class Todotask
{
public string Name { get; set; }
public string Note { get; set; }
public DateTime LastEdit { get; set; }
public bool Finished { get; set; }
}
And you can Deserialize it:
static void Main(string[] args)
{
var query = JsonSerializer.Deserialize<Rootobject>(File.ReadAllText("data.json"));
}
Your json has a root object containing the task list, so List<TaskList> does not represent it correctly. Try:
public class Root
{
public List<TaskList> TaskLists { get; set; }
}
var root = JsonSerializer.Deserialize<Root>(json);

How to map json response to the model with different field names

I am using an ASP.NET Core 6 and System.Text.Json library.
For example, I'm getting a response from the some API with the following structure
{
"items":
[
{
"A": 1,
"User":
{
"Name": "John",
"Age": 21,
"Adress": "some str"
},
},
{
"A": 2,
"User":
{
"Name": "Alex",
"Age": 22,
"Adress": "some str2"
},
}
]
}
And I want to write this response to the model like List<SomeEntity>, where SomeEntity is
public class SomeEntity
{
public int MyA { get; set; } // map to A
public User MyUser { get; set; } // map to User
}
public class User
{
public string Name { get; set; }
public string MyAge { get; set; } // map to Age
}
How could I do it?
UPDATE:
Is it possible to map nested properties?
public class SomeEntity
{
// should I add an attribute [JsonPropertyName("User:Name")] ?
public string UserName{ get; set; } // map to User.Name
}
Use the JsonPropertyName attribute
public class Model
{
[JsonPropertyName("items")]
public SomeEntity[] Items { get; set; }
}
public class SomeEntity
{
[JsonPropertyName("A")]
public int MyA { get; set; } // map to A
[JsonPropertyName("User")]
public User MyUser { get; set; } // map to User
}
public class User
{
public string Name { get; set; }
[JsonPropertyName("Age")]
public string MyAge { get; set; } // map to Age
}
You can then deserialize it with something like
JsonSerializer.Deserialize<Model>(response);
try this please
[JsonConverter(typeof(JsonPathConverter))]
public class SomeEntity
{
[JsonProperty("items.User.Name")]
public string UserName{ get; set; } // map to User.Name
}
deserialize using :
JsonSerializer.Deserialize<SomeEntity>(response);

Pass Nested Deserialized JSON from Controller to View

After using HttpClient class to convert my JSON to a string and deserialize it with
var response = Res.Content.ReadAsStringAsync().Result;
data = JsonConvert.DeserializeObject<List<Employee>>(response);
How do I pass the data that I receive in the Controller from the call using the Model below to the View?
public class RuleType
{
public int Id { get; set; }
public string Description { get; set; }
public bool Inactive { get; set; }
}
public class RuleCategory
{
public int Id { get; set; }
public string Description { get; set; }
public bool Inactive { get; set; }
}
public class Employee
{
public string Description { get; set; }
public object EndDateTime { get; set; }
public int Id { get; set; }
public bool Inactive { get; set; }
public int RuleAction { get; set; }
public DateTime StartDateTime { get; set; }
public RuleType RuleType { get; set; }
public RuleCategory RuleCategory { get; set; }
}
Here is one object from the call
[
{
"Description": "Test Description",
"EndDateTime": null,
"Id": 1,
"Inactive": false,
"RuleAction": -2,
"StartDateTime": "2017-01-06T14:58:58Z",
"RuleType": {
"Id": 6,
"Description": "Test Description",
"Inactive": false
},
"RuleCategory": {
"Id": 1,
"Description": "Description",
"Inactive": false
}
}
]
Not sure if I'm missing something, but if you have an object you want to return to the view from the controller, you simply:
return View(viewModel); // in your case viewModel = 'data'
As others have said here already, you should be deserializing the JSON into a RootObject instead of an Employee like so:
var response = Res.Content.ReadAsStringAsync().Result;
var data = JsonConvert.DeserializeObject<List<RootObject>>(response);
You can then pass the model into the view using just:
return View(data)
You should also consider renaming RootObject into something more useful (such as employee?) as RootObject is not a very useful or descriptive name.

Deserializing Json Schema to Json String or Object

I have a json schema and I need to convert it to a C# object or at least into json string.
is there any way to do it by code or by using some tool?
for the Json I'm currently using Json.net.
this is one of my schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "UserGroupWsDTO",
"type": "object",
"properties":
{
"members":
{
"type": "array",
"items":
{
"type": "object",
"properties":
{
"uid":
{
"type": "string"
}
}
}
},
"uid":
{
"type": "string"
},
"name":
{
"type": "string"
}
}
}
I need this to create an Object for deserialize the json
EDIT
My Json schema version is 4 and JSON Schema to POCO doesn't support it
Have a look at JSON Schema to POCO which supports v3 JSON.
If you are just "browsing" key-values, then you don't need any extra libs...
just do:
var obj = (JObject)JsonConvert.DeserializeObject(json);
var dict = obj.First.First.Children().Cast<JProperty>()
.ToDictionary(p => p.Name, p =>p.Value);
var dt = (string)dict["title"];
but if instead you need an object of the string, then define a class and deserialize the string to that class... follow this example:
1st define the classes:
public class Uid
{
public string type { get; set; }
}
public class Properties2
{
public Uid uid { get; set; }
}
public class Items
{
public string type { get; set; }
public Properties2 properties { get; set; }
}
public class Members
{
public string type { get; set; }
public Items items { get; set; }
}
public class Uid2
{
public string type { get; set; }
}
public class Name
{
public string type { get; set; }
}
public class Properties
{
public Members members { get; set; }
public Uid2 uid { get; set; }
public Name name { get; set; }
}
public class RootObject
{
public string __invalid_name__$schema { get; set; }
public string title { get; set; }
public string type { get; set; }
public Properties properties { get; set; }
}
and this is the implementation:
string json = #"{...use your json string here }";
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(root.title);
// UserGroupWsDTO

What is the correct class structure in C# to convert this Json into?

I have the following Json below coming from a Rest service and I am trying to deserialize it into a C# object using this code:
var _deserializer = new JsonDeserializer();
var results = _deserializer.Deserialize<Report>(restResponse);
The deserialize method keeps returning null which tells me that my C# object is not structured correctly.
Below is the Json and my latest attempt at the C# definition.
{
"Report": [
{
"ID": "0000014",
"Age": "45",
"Details": [
{
"Status": "Approved",
"Name": "Joe"
},
{
"Status": "Approved",
"Name": "Bill"
},
{
"Status": "Submitted",
"Name": "Scott"
}
]
},
{
"ID": "10190476",
"Age": "40",
"Details": [
{
"Status": "Approved",
"Name": "Scott"
}
]
},
{
"ID": "10217480",
"Age": "40",
"Details": [
{
"Status": "Approved",
"Name": "Scott"
}
]
}
]
}
Here is my C# object:
public class Report
{
public List<WorkItem> Item= new List<WorkItem>();
}
public class WorkItem
{
public string ID { get; set; }
public int Age { get; set; }
public List<Details> Details { get; set; }
}
public class Details
{
public string Status { get; set; }
public string Name { get; set; }
}
Can someone advise what is wrong with my C# object definition to make this json deserialize correctly?
I would recommend using Json2Csharp.com to generate the classes.
public class Detail
{
public string Status { get; set; }
public string Name { get; set; }
}
public class Report
{
public string ID { get; set; }
public string Age { get; set; }
public List<Detail> Details { get; set; }
}
public class RootObject
{
public List<Report> Report { get; set; }
}
Try changing the Report class like so (The class name can be anything, the property must be Report)
public class WorkReport
{
public List<WorkItem> Report;
}
It should be trying to deserialize at the root into a class with an array/list of of workitem objects called Report.
You can try something like this. I have changed List to Dictionary You don't have a class defined at the root level. The class structure needs to match the entire JSON, you can't just deserialize from the middle. Whenever you have an object whose keys can change, you need to use a Dictionary. A regular class won't work for that; neither will a List.
public class RootObject
{
[JsonProperty("Report")]
public Report Reports { get; set; }
}
public class Report
{
[JsonProperty("Report")]
public Dictionary<WorkItem> Item;
}
public class WorkItem
{
[JsonProperty("ID")]
public string ID { get; set; }
[JsonProperty("Age")]
public int Age { get; set; }
[JsonProperty("Details")]
public Dictionary<Details> Details { get; set; }
}
public class Details
{
[JsonProperty("Status")]
public string Status { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
}
Then, deserialize like this:
Report results = _deserializer.Deserialize<Report>(restResponse);

Categories

Resources