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")
}
Related
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>>();
We have json format as shown below and want to normalize it as given in expected output.
Input format:
[
{
"country": "Germany",
"name": "2010",
"value": 40632
},
{
"country": "United States",
"name": "2010",
"value": 0
},
{
"country": "United States",
"name": "2000",
"value": 45986
},
{
"country": "United States",
"name": "1990",
"value": 37060
},
{
"country": "France",
"name": "2010",
"value": 36745
},
{
"country": "France",
"name": "2000",
"value": 34774
}
]
Expected output :
[
{
"name": "Germany",
"series": [
{
"name": "2010",
"value": 40632
}
]
},
{
"name": "United States",
"series": [
{
"name": "2010",
"value": 0
},
{
"name": "2000",
"value": 45986
},
{
"name": "1990",
"value": 37060
}
]
},
{
"name": "France",
"series": [
{
"name": "2010",
"value": 36745
},
{
"name": "2000",
"value": 34774
}
]
}
]
try this
var jArr = JArray.Parse(input);
var groupedData = jArr.GroupBy(a => a["country"]).ToList();
var outputArr = new JArray();
foreach (var item in groupedData)
{
JObject obj = new JObject();
obj.Add("name", item.Key);
var arr = new JArray();
obj.Add("series", arr);
foreach (var jObj in item)
{
JObject newObj = new JObject();
newObj.Add("name", jObj["name"]);
newObj.Add("value", jObj["value"]);
arr.Add(newObj);
}
outputArr.Add(obj);
}
var output = outputArr.ToString();
I am looking for a script to find the value of $6383.12 for Accounts Receivable (A/R) in this code. There are several values I want to be able to find but I can't seem to figure out how to structure my code to find the values I need.
I have spent time looking through and testing various versions of arrays, ILIst<> and other suggestions but I can't seem to get the final result I am looking for. I can find a single value (for example "Savings") but I don't know how to get the $800 value.
The script I am using is:
var root = JToken.Parse(data);
IList<JToken> t = root.SelectTokens("$...ColData[?(#.value == 'Accounts Receivable (A/R)')]").ToList();
foreach (var item in t)
{
Response.Write(item.ToString() + "<br/><br/>");
}
This gives me the Accounts Receivable (A/R) value but not the dollar value associated with it.
Here is the JSON result I am trying to parse through:
{
"Header": {
"ReportName": "BalanceSheet",
"Option": [
{
"Name": "AccountingStandard",
"Value": "GAAP"
},
{
"Name": "NoReportData",
"Value": "false"
}
],
"DateMacro": "this calendar year-to-date",
"ReportBasis": "Accrual",
"StartPeriod": "2016-01-01",
"Currency": "USD",
"EndPeriod": "2016-10-31",
"Time": "2016-10-31T09:42:21-07:00",
"SummarizeColumnsBy": "Total"
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "ASSETS"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Bank Accounts"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "35",
"value": "Checking"
},
{
"value": "1350.55"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "36",
"value": "Savings"
},
{
"value": "800.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "BankAccounts",
"Summary": {
"ColData": [
{
"value": "Total Bank Accounts"
},
{
"value": "2150.55"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Accounts Receivable"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "84",
"value": "Accounts Receivable (A/R)"
},
{
"value": "6383.12"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "AR",
"Summary": {
"ColData": [
{
"value": "Total Accounts Receivable"
},
{
"value": "6383.12"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Other current assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "81",
"value": "Inventory Asset"
},
{
"value": "596.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "4",
"value": "Undeposited Funds"
},
{
"value": "2117.52"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "OtherCurrentAssets",
"Summary": {
"ColData": [
{
"value": "Total Other current assets"
},
{
"value": "2713.77"
}
]
}
}
]
},
"type": "Section",
"group": "CurrentAssets",
"Summary": {
"ColData": [
{
"value": "Total Current Assets"
},
{
"value": "11247.44"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Fixed Assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"id": "37",
"value": "Truck"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "38",
"value": "Original Cost"
},
{
"value": "13495.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"Summary": {
"ColData": [
{
"value": "Total Truck"
},
{
"value": "13495.00"
}
]
}
}
]
},
"type": "Section",
"group": "FixedAssets",
"Summary": {
"ColData": [
{
"value": "Total Fixed Assets"
},
{
"value": "13495.00"
}
]
}
}
]
},
"type": "Section",
"group": "TotalAssets",
"Summary": {
"ColData": [
{
"value": "TOTAL ASSETS"
},
{
"value": "24742.44"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "LIABILITIES AND EQUITY"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Accounts Payable"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "33",
"value": "Accounts Payable (A/P)"
},
{
"value": "1984.17"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "AP",
"Summary": {
"ColData": [
{
"value": "Total Accounts Payable"
},
{
"value": "1984.17"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Credit Cards"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "41",
"value": "Mastercard"
},
{
"value": "157.72"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "CreditCards",
"Summary": {
"ColData": [
{
"value": "Total Credit Cards"
},
{
"value": "157.72"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Other Current Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "89",
"value": "Arizona Dept. of Revenue Payable"
},
{
"value": "4.55"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "90",
"value": "Board of Equalization Payable"
},
{
"value": "401.98"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "43",
"value": "Loan Payable"
},
{
"value": "4000.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "OtherCurrentLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Other Current Liabilities"
},
{
"value": "4406.53"
}
]
}
}
]
},
"type": "Section",
"group": "CurrentLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Current Liabilities"
},
{
"value": "6548.42"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Long-Term Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "44",
"value": "Notes Payable"
},
{
"value": "25000.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "LongTermLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Long-Term Liabilities"
},
{
"value": "25000.00"
}
]
}
}
]
},
"type": "Section",
"group": "Liabilities",
"Summary": {
"ColData": [
{
"value": "Total Liabilities"
},
{
"value": "31548.42"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Equity"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "34",
"value": "Opening Balance Equity"
},
{
"value": "-9337.50"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "2",
"value": "Retained Earnings"
},
{
"value": "91.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Net Income"
},
{
"value": "2440.27"
}
],
"type": "Data",
"group": "NetIncome"
}
]
},
"type": "Section",
"group": "Equity",
"Summary": {
"ColData": [
{
"value": "Total Equity"
},
{
"value": "-6805.98"
}
]
}
}
]
},
"type": "Section",
"group": "TotalLiabilitiesAndEquity",
"Summary": {
"ColData": [
{
"value": "TOTAL LIABILITIES AND EQUITY"
},
{
"value": "24742.44"
}
]
}
}
]
},
"Columns": {
"Column": [
{
"ColType": "Account",
"ColTitle": "",
"MetaData": [
{
"Name": "ColKey",
"Value": "account"
}
]
},
{
"ColType": "Money",
"ColTitle": "Total",
"MetaData": [
{
"Name": "ColKey",
"Value": "total"
}
]
}
]
}
}
You can try this,
var json = File.ReadAllText("json1.json");
var jToken = JToken.Parse(json);
var reader = jToken.CreateReader();
while (reader.Read())
{
var value = reader.Value;
if (value != null && value.ToString() == "Accounts Receivable (A/R)")
{
var test = jToken.SelectToken(reader.Path.Replace("[0].value", "[1].value"));
}
}
If it's not doable to write json path which selects proper tokens you could try using Parent property and Children method.
foreach (var item in t)
{
var valueToken = item.Parent.Children().ElementAt(1);
Response.Write(valueToken.ToString() + "<br/><br/>");
}
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 want to get the FILE-file-id, FILE-fileSize FILENAME-id, INCIDENT-reportedOn out of the following JObject:
Note the two "[[" at the beginning. Do I have to reduce the JObject first?
[
[{
"FILENAME": {
"id": "renamedtopdf.docx.pdf",
"label": "fileName",
"type": "vertex"
},
"FILE": {
"id": "dc92d48b7e29c528b3eb168446e51736101122a821c9e712320bd6842116719a",
"label": "file",
"type": "vertex",
"properties": {
"fileSize": [{
"id": "f9339436-189a-4503-abc6-e2989be6f138",
"value": "164198"
}],
"mimeType": [{
"id": "0a89dbfa-c204-45c8-8524-3fbd02b04e39",
"value": "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
}]
}
},
"INCIDENT": {
"id": "16ea8c8b-65ee-44b3-afbb-98308b092b4f",
"label": "incident",
"type": "vertex",
"properties": {
"reportedOn": [{
"id": "81485296-a62f-4d17-a03f-4995c3cad937",
"value": "2/16/2019 10:33:59 AM"
}]
}
}
},
I'll assume you already extracted the JObject from the two arrays. I that case you can simply use the index operator to traverse the json file like so:
json["FILE"]["id"].Value<string>();
json["FILE"]["properties"]["fileSize"]["value"].Value<string>();
json["FILENAME"]["id"].Value<string>();
json["INCIDENT"]["properties"]["reportedOn"]["Value"].Value<string>();
Full Example:
const string text = #"{
"FILENAME": {
"id": "renamedtopdf.docx.pdf",
"label": "fileName",
"type": "vertex"
},
"FILE": {
"id": "dc92d48b7e29c528b3eb168446e51736101122a821c9e712320bd6842116719a",
"label": "file",
"type": "vertex",
"properties": {
"fileSize": [
{
"id": "f9339436-189a-4503-abc6-e2989be6f138",
"value": "164198"
}
],
"mimeType": [
{
"id": "0a89dbfa-c204-45c8-8524-3fbd02b04e39",
"value": "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
}
]
}
},
"INCIDENT": {
"id": "16ea8c8b-65ee-44b3-afbb-98308b092b4f",
"label": "incident",
"type": "vertex",
"properties": {
"reportedOn": [
{
"id": "81485296-a62f-4d17-a03f-4995c3cad937",
"value": "2/16/2019 10:33:59 AM"
}
]
}
}
}";
var json = JObject.Parse(text);
json["FILE"]["id"].Value<string>();
json["FILE"]["properties"]["fileSize"]["value"].Value<string>();
json["FILENAME"]["id"].Value<string>();
json["INCIDENT"]["properties"]["reportedOn"]["Value"].Value<string>();