Reputation: 3
Based on the validated JSON below:
{
"GrowthRates": [{
"FinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 11096,
"txt": "Between 0 and 12 months"
}
},
"ComparedToFinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 13711,
"txt": "1-3 years"
}
},
"GrowthRateItem": [{
"ItemDescriptionText": {
"attrCodeValue": 6240,
"txt": "Total number of employees"
},
"ItemRate": 0
},
{
"ItemDescriptionText": {
"attrCodeValue": 3110,
"txt": "Sales Revenue"
},
"ItemRate": 0
}
]
},
{
"FinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 11096,
"txt": "Between 0 and 12 months"
}
},
"ComparedToFinancialStatementHeader": {
"TimePeriodText": {
"attrCodeValue": 13721,
"txt": "1-5 years"
}
},
"GrowthRateItem": [{
"ItemDescriptionText": {
"attrCodeValue": 6240,
"txt": "Total number of employees"
},
"ItemRate": 0
},
{
"ItemDescriptionText": {
"attrCodeValue": 3110,
"txt": "Sales Revenue"
},
"ItemRate": 0
}
]
}
]
}
I need to retrieve the ItemRate value as a string where the GrowthRates array property, ["ComparedToFinancialStatementHeader"]["TimePeriodText"]["txt"]
, value is a specified value in this case "1-3 years" or "1-5 years" and the GrowthRateItem
array property, ["ItemDescriptionText"]["txt"]
, value is also a specified value in this case "Total number of employees" or "Sales Revenue".
The LINQ to JSON query only needs to implement one case of the four cases implied by the four values listed above.
I have not been able to extend the following solution used to solve a similar problem.
LINQ Query to retrieve an inner array value based on two property values from the outer array.
JToken jsonTkn = JToken.Parse(jsonString);
string jsonValue = jsonTkn["IndustryCode"]
.Where(icItem => icItem["attrTypeText"].Value<string>() == "NAICS" && icItem["DisplaySequence"].Value<string>() == "1")
.Select(icItem => icItem["IndustryCodeDescription"].First()["txt"].Value<string>())
.First().ToString().ToUpper();
JSON string:
{
"IndustryCode": [{
"attrCodeValue": 25838,
"attrTypeText": "DBH Industry Code",
"IndustryCode": {
"txt": "1063"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Legal Services"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 700,
"attrTypeText": "NAICS",
"IndustryCode": {
"txt": "541110"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Offices of Lawyers"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 24664,
"attrTypeText": "North American Industry Classification System 2012",
"IndustryCode": {
"txt": "541110"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Offices of Lawyers"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 21182,
"attrTypeText": "UK SIC 2003",
"IndustryCode": {
"txt": "74.110"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 9120,
"txt": "Legal activities"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 3599,
"attrTypeText": "DBS Industry Code",
"IndustryCode": {
"txt": "81119901"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 2121,
"txt": "GENRL PRACTICE ATTY"
}],
"DisplaySequence": 1
},
{
"attrCodeValue": 399,
"attrTypeText": "US Standard Industry Code 1987 - 4 digit",
"IndustryCode": {
"txt": "8111"
},
"IndustryCodeDescription": [{
"attrLanguageCode": 39,
"attrIndustryCodeDescriptionLengthCode": 1441,
"txt": "Legal services office"
}],
"DisplaySequence": 1
}
]
}
I listed the above solution to show the structure in which I am trying to solve the posed question.
Upvotes: -1
Views: 100
Reputation: 129677
How about something like this?
public static string GetItemRate(JObject obj, string timePeriodText, string itemDescriptionText)
{
return obj.SelectToken("GrowthRates")
.First(jo => (string)jo.SelectToken("ComparedToFinancialStatementHeader.TimePeriodText.txt") == timePeriodText)
.SelectToken("GrowthRateItem")
.First(jo => (string)jo.SelectToken("ItemDescriptionText.txt") == itemDescriptionText)
.SelectToken("ItemRate")
.Value<string>();
}
Then use it like this:
JObject obj = JObject.Parse(jsonString);
string rate = GetItemRate(obj, "1-3 years", "Sales Revenue");
Fiddle: https://dotnetfiddle.net/KVslv2
Upvotes: 0