I have the following JSON that is returned to me from an API call. I’m trying to just get the information from a property called “products”. Once I have that into an array or list I need to look for a specific product name and then one of it’s properties.
I’ve tried a handful of different things without any luck. I’m also searching and not finding what I’m looking for in my scenario.
If I use the following, I can get the data into parseJson object but from there I don’t understand how I can pull just the “products” into an array so I can loop through them looking for a specific product and it’s value.
dynamic parseJson = JsonConvert.DeserializeObject(response.Content);
I also tried this but had no luck either.
dynamic parseJson = JsonConvert.DeserializeObject<AccurateApiResult>(response.Content);
public class AccurateApiResult
{
public List<AccurateProduct> products { get; set; }
}
public class AccurateProduct
{
public int id { get; set; }
public string productType { get; set; }
public string status { get; set; }
public string result { get; set; }
public bool flag { get; set; }
}
Here is the sample data and I’m only interested in the “products” section. How can I pull just that data?
[
{
"resource": "ORDER",
"id": "Y10046727",
"created": "2022-06-28T13:18:17Z",
"updated": "2022-06-28T13:18:17Z",
"workflow": "INTERACTIVE",
"candidate": {
"firstName": "Marcus",
"lastName": "Willis",
"middleName": null,
"suffix": null,
"dateOfBirth": "1990-01-01",
"ssn": "111111111",
"email": null,
"phone": "240-5798551",
"address": "3433 Lumar dr",
"city": "Fort Washington",
"region": "MD",
"country": "US",
"postalCode": "20744",
"governmentId": {
"country": "US",
"type": null,
"number": null
},
"aliases": [],
"educations": [
{
"school": "Test University",
"country": "US",
"region": "CA",
"city": "Irvine",
"degree": null,
"major": null,
"startDate": null,
"endDate": null,
"graduated": false,
"graduationDate": null,
"presentlyEnrolled": false
}
],
"prevEmployed": null,
"employments": [],
"convicted": null,
"convictions": null,
"references": [],
"addressHistory": []
},
"completed": "2022-07-07T01:59:13Z",
"supportReferenceId": "Y10046727",
"status": "COMPLETE",
"result": "Meets Requirements",
"products": [
{
"id": 66134505,
"productType": "AELS",
"status": "COMPLETE",
"result": "NOT APPLICABLE",
"flag": false
},
{
"id": 66134506,
"productType": "ADJ",
"status": "COMPLETE",
"result": "NOT APPLICABLE",
"flag": false
},
{
"id": 66134508,
"productType": "MOV",
"status": "COMPLETE",
"result": "Invalid SSN",
"flag": false
},
{
"id": 66144583,
"productType": "MVR",
"status": "COMPLETE",
"result": "NOT APPLICABLE",
"flag": false
},
{
"id": 66144584,
"productType": "F/M",
"status": "COMPLETE",
"result": "NO RECORD FOUND",
"flag": false
},
{
"id": 66144587,
"productType": "EDU",
"status": "COMPLETE",
"result": "NOT APPLICABLE",
"flag": false
},
{
"id": 66144588,
"productType": "DL5D",
"status": "COMPLETE",
"result": "Negative",
"flag": false
}
],
"percentageComplete": 0,
"candidateInfoChanged": false,
"searchId": 66134503,
"subjectId": 10121219,
"requestor": "maegan#email.com"
}
you can try this code
var parsedJsonProducts = (JArray) JArray.Parse(response.Content)[0]["products"];
List<AccurateProduct> products = parsedJsonProducts.ToObject<List<AccurateProduct>>();
Related
I have following MongoDB document:
{
"_id": {
"$oid": "5fbfa0005c15aaf2eac69ba6"
},
"PostMedia": [
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}
]
},
{
"_id": {
"$oid": "5fbfa0485c15aaf2eac69ba7"
},
"PostMedia": [
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
}
and I want to fetch all PostMedia to single array using MongoDB C# driver.
Here is the expected result:
[
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}, {
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
I had tried to use group aggregation function but it returned an array of array.
Result I received:
PostMedia:[{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img1.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img2.jpg",
"Title": null,
"Description": ""
}],[
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img3.jpg",
"Title": null,
"Description": ""
},
{
"FilePath": "http://localhost:8886/localhost44323/image/",
"Filename": "img4.jpg",
"Title": null,
"Description": ""
}
]
C# code I have written so far is as follows:
var group = new[]
{
new BsonDocument("$group",
new BsonDocument
{
{ "_id", BsonNull.Value },
{ "PostMedia", new BsonDocument("$push", "$PostMedia") }
})
};
var result = collection.Aggregate<MediaList>(group).FirstOrDefault();
return list;
}
Is there any way to fetch subdocument by merging them is a single array.
You have to $unwind before $group,
$unwind deconstruct PostMedia array
$replaceRoot replace PostMedia object to root
$unset remove _id field
{ $unwind: "$PostMedia" },
{
$group: {
_id: null,
PostMedia: { $push: "$PostMedia" }
}
},
{ $unset: "_id" } // remove field
Playground
C#:
new BsonArray
{
new BsonDocument("$unwind", "$PostMedia"),
new BsonDocument("$group",
new BsonDocument
{
{ "_id", BsonNull.Value },
{ "PostMedia",
new BsonDocument("$push", "$PostMedia") }
})
new BsonDocument("$unset", "_id")
}
I have a problem deserializing a json file, this is the json:
[
{
"id": "id",
"number": "48",
"date": "17-01-2020",
"details": [
{
"id": "id",
"code": "code",
"description": "desc"
},
{
"id": "id",
"code": "code",
"description": "desc"
}
],
"address": "add",
"note": null
},
{
"id": "id",
"number": "55",
"date": "17-01-2020",
"details": [
{
"id": "id",
"code": "code",
"description": "desc"
},
{
"id": "id",
"code": "code",
"description": "desc"
}
],
"address": "add",
"note": null
}
]
this is my code:
var result = httpClient.GetAsync(".....").Result;
List<Docu> doc= new JavaScriptSerializer().Deserialize<List<Docu>>(result.Content.ReadAsStringAsync().Result);
class Docu contains definition of id, number, date, details and:
public List<Details> det{ get; set; }
Class Details contains id, code and description definition
I can deserialize everything except complex object details, it returns null from deserialization, how I can fix this? I need to fill the list of details
You have wrong name for List<Details> property
it should be
public List<Details> details{ get; set; }
according to json you have shown
This question already has answers here:
Remove duplicates in the list using linq
(11 answers)
Closed 2 years ago.
I am very new to LINQ. I have run to a scenario where I need to find distinct from an object and remove the others. Please help.
I have tried like this
var xyz = referal.GroupBy(i => i.Select(x => x.identifier).Distinct().Select(g=>g));
but it is not returning the distinct values. Thanks in advance.
Sample Object :
{
"referral_type": [
{
"type": "MOBILE",
"invitee": [
{
"identifier": "12345678",
"name": "Test2",
"invited_on": "2020-01-29 12:25:46.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "98765432",
"name": "Test1",
"invited_on": "2020-01-29 13:37:36.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "12345678",
"name": "Harry Test",
"invited_on": "2020-01-29 13:55:32.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "98765432",
"name": "Harry Test",
"invited_on": "2020-01-29 13:55:32.0",
"till": {
"code": "",
"name": ""
}
}
]
}
]
}
So I want to check the distinct identifier and take the first one irresoective of other values, something like this
{
"referral_type": [
{
"type": "MOBILE",
"invitee": [
{
"identifier": "12345678",
"name": "Test2",
"invited_on": "2020-01-29 12:25:46.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "98765432",
"name": "Test1",
"invited_on": "2020-01-29 13:37:36.0",
"till": {
"code": "",
"name": ""
}
}
]
}
]
}
You need to implement the interface IEquatable<T> class "invitee", because Dinstinct use this to compare object. On this way i think you are comparing memory references.
i have a Json file that contains some information i need elsewhere in my code but a lot of the information is irrelivant.
At the moment ive just put it into a dynamic object so i could check that it was all working:
var data = JsonConvert.DeserializeObject<dynamic>(response.Content);
How do i get the information i need out of the Json file and store them somewhere as variables.
All other tutorials where its stored in a class use the whole Json file and doesnt look like it would be useful in my case.
Here's the Json, i only really need the Stats section at the end of the file for what im doing
{
"data": {
"id": "",
"type": "player",
"children": [
{
"id": "legend_8",
"type": "legend",
"metadata": {
"legend_name": "Pathfinder",
"icon": "https://trackercdn.com/cdn/apex.tracker.gg/legends/pathfinder-tile.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/pathfinder-concept-bg-small.jpg",
"is_active": true
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 377.0,
"percentile": 21.0,
"displayValue": "377",
"displayRank": ""
},
{
"metadata": {
"key": "Finishers",
"name": "Finishers",
"categoryKey": "game",
"categoryName": "Game",
"isReversed": false
},
"value": 39.0,
"percentile": 0.2,
"rank": 886,
"displayValue": "39",
"displayRank": "886"
}
]
},
{
"id": "legend_5",
"type": "legend",
"metadata": {
"legend_name": "Bloodhound",
"icon": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bloodhound-tile.png",
"bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bloodhound-concept-bg-small.jpg",
"is_active": false
},
"stats": [
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 235.0,
"percentile": 16.0,
"displayValue": "235",
"displayRank": ""
}
]
}
],
"metadata": {
"statsCategoryOrder": [
"combat",
"game",
"weapons"
],
"platformId": 2,
"platformUserHandle": "",
"accountId": "",
"cacheExpireDate": "11/10/2019 10:48:14 PM",
"level": 49,
"avatarUrl": "https://avatar-cdn.tracker.gg/api/avatar/2/",
"countryCode": null,
"collections": 36,
"activeLegend": 8
},
"stats": [
{
"metadata": {
"key": "Level",
"name": "Level",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 49.0,
"percentile": 46.0,
"displayValue": "49",
"displayRank": ""
},
{
"metadata": {
"key": "Kills",
"name": "Kills",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 612.0,
"percentile": 20.0,
"displayValue": "612",
"displayRank": ""
},
{
"metadata": {
"key": "Finishers",
"name": "Finishers",
"categoryKey": "game",
"categoryName": "Game",
"isReversed": false
},
"value": 39.0,
"percentile": 0.5,
"displayValue": "39",
"displayRank": ""
},
{
"metadata": {
"key": "RankScore",
"name": "Rank Score",
"categoryKey": "game",
"categoryName": "Game",
"isReversed": false
},
"value": 64.0,
"percentile": 21.0,
"displayValue": "64",
"displayRank": ""
}
]
}
}
You could create a data structure which has only the relevant properties. For example,
public class StatMetaData
{
public string key { get; set; }
public string name { get; set; }
public string categoryKey { get; set; }
public string categoryName { get; set; }
public bool isReversed { get; set; }
}
public class Stat
{
public StatMetaData metadata { get; set; }
public double value { get; set; }
public double percentile { get; set; }
public string displayValue { get; set; }
public string displayRank { get; set; }
}
public class Data
{
public List<Stat> stats { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
Now you could deserialize the json as the following to retrieve the stats sections
var result = JsonConvert.DeserializeObject<RootObject>(json).data.stats;
I am new to JSON and trying to develop a comparison table with data coming from 2 different JSON files as follows.
Json File 1
"data":[
{
"EffectiveDate": "2017-04-01T00:00:00Z",
"IncludedQuantity": 0,
"Category": "VM",
"Id": "d0bf9053",
"Name": "Element1",
"Rates": {
"0": 0.04
},
"Region": "US",
"Status": "Active",
"SubCategory": "S1",
"Tags": [],
"Unit": "Hours"
},
{
"EffectiveDate": "2017-02-01T00:00:00Z",
"IncludedQuantity": 0,
"Category": "DS",
"Id": "8b7672d4",
"Name": "Element2",
"Rates": {
"0": 4.0177
},
"Region": "UK",
"Status": "Active",
"SubCategory": "S2",
"Tags": [],
"Unit": "Days"
}]
Json File 2
"data":{
"d0bf9053":{
"EffectiveDate": "2017-04-01T00:00:00Z",
"IncludedQuantity": 0,
"Category": "VM",
"Id": "d0bf9053",
"Attributes":{
"Name": "Element1",
"Rates": {
"0": 5
},
"Region": "US",
"Status": "Active",
"SubCategory": "S1",
"Tags": [],
"Unit": "Hours"
}
},
"8b7672d4":{
"EffectiveDate": "2017-02-01T00:00:00Z",
"IncludedQuantity": 0,
"Category": "DS",
"Id": "8b7672d4",
"Attributes":{
"Name": "Element2",
"Rates": {
"0": 1
},
"Region": "UK",
"Status": "Active",
"SubCategory": "S2",
"Tags": [],
"Unit": "Days"
}
}}
Now I have to read this data and merge it to create a comparison based on rates and categories. I have created a Dot.Net Console Application and trying to use SQL Server to store the information. Now I am able to read JSON File 1 easily. But I am facing difficulty in storing values from JSON File 2. How do I solve this or am I wrong somewhere.
Thanks in advance.
The problem here is that you have dynamic property names in the second JSON file (Json File 2) and you are probably having trouble deserializing that JSON into a .Net Class, right?
Here are some examples that you can use to deserialize both Json1 and Json2 Types:
public class dataJson1
{
public List<Detail> data { get; set; }
}
public class dataJson2
{
public Dictionary<string, Detail> data { get; set; }
}
public class Detail
{
public DateTime EffectiveDate { get; set; }
public int IncludedQuantity { get; set; }
public string Category { get; set; }
//add the rest of the props here
}
That should get you started, once it's deserialized into those Objects, comparing them should be trivial.