Hi,
How to query this nested json ?
[{
"_id": {
"$oid": "65af63b9b90f28b3176dbae8"
},
"Company Name": "ABCD",
"Location": "Mysore",
"NoOfEmployee": "300",
"fixlevel": 10,
"create": "1",
"prod1": [
{
"wre": {
"id": "wre",
"name": "wrt",
"address": "345 435435vr",
"pincode": "535345"
}
},
{
"1q": {
"id": "1q",
"name": "re
"address": "ewrwer",
"pincode": "213414"
}
}
],
"prod2": [
{
"22": {
"id": "22",
"name": "423",
"address": "erw",
"pincode": "324234",
"linkstring": "prod1.wre",
"linkvalue": "wre"
}
},
"223": {
"id": "223",
"name": "sdfsdf",
"meterid": "erwer",
"address": "fgsdgf",
"pincode": "321114",
"linkstring": "prod1.1q",
"linkvalue": "1q"
}
]
}]

Amit MohantyPosted Dec 10, 2024, 11:08 AM
Try this once:
Sangeetha SPosted Dec 10, 2024, 10:39 AM
Hi,
var filter = Builders.Filter.ElemMatch("prod1", Builders.Filter.Eq("wre.name", "wrt"));
inside the prod1 - "wre" Key is not same every time. each time it will change.
"wre" is dynamic here How to form query ?
Jaish MathewsPosted Dec 10, 2024, 10:38 AM
To query the nested JSON using
System.Text.Json, which is the built-in JSON parser in .NET, you can take advantage of itsJsonDocumentandJsonArrayclasses for efficient parsing and querying. Here's an approach usingSystem.Text.Json.Steps:
Install
System.Text.Json: It's part of the .NET Core/Framework SDK, so no need for extra installations if you're using .NET 5 or later.Use
JsonDocumentto parse and query the JSON.Code Example Using
System.Text.Json:Explanation:
JsonDocument.Parse: Parses the JSON string into aJsonDocument, which is used for querying and manipulating JSON in a read-only way.EnumerateArray(): Converts aJsonArrayelement into an enumerator so you can loop through its elements.GetProperty(): Extracts specific properties from JSON objects, e.g.,company.GetProperty("Company Name").GetString().TryGetProperty(): Used for checking and extracting properties from nested JSON objects likeprod1orprod2.Output:
Benefits of Using
System.Text.Json:System.Text.Jsonis faster and uses less memory than Newtonsoft.Json.JsonDocumentfor parsing JSON in a non-modifying way.Limitations:
System.Text.Jsondoesn't have the same extensive features as Newtonsoft.Json (e.g., serialization/deserialization with custom converters), but for querying and accessing JSON, it provides a high-performance, built-in solution.For more advanced querying or if you need to manipulate the JSON data, Newtonsoft.Json remains a flexible option, but
System.Text.Jsonis a great choice for performance-critical scenarios or when working with built-in .NET features.Jayraj ChhayaPosted Dec 10, 2024, 10:26 AM
Hi Sangeetha S,
To query a nested JSON structure in MongoDB using C#, you can utilize the MongoDB C# driver, which provides a straightforward way to interact with your database. Given the provided JSON structure, you can access nested fields using the dot notation. Below is an example of how to query the
prod1array for a specificname:We create a filter to match documents where the
namefield in theprod1array'swreobject equals "wrt". This approach allows you to efficiently retrieve nested data from your MongoDB collection.