Return item from JSON content row based on value

Probably doing something weird but. I have a JSONObject containing 2 tables, one of which is “report”
I want to return te number of lines for a specific “status” to a string but it can’t be
JSONContent(“report”)(1)(“numberoflines”) (which does work) as I can’t be sure of the sort.
So I want to filter on the row “status”: “Checked”
So I need something like JSONContent(“report”).AsEnumerable().Where(Function(r) r(“status”).ToString = “Checked”)(“numberoflines”) but can’t figure it out.

What am I missing?

sample:
JSONContent(“report”)
[
{
“status”: “Failed”,
“numberoflines”: 0
},
{
“status”: “Checked”,
“numberoflines”: 1
},
{
“status”: “TotalLines”,
“numberoflines”: 1
}
]

Hi,

How about the following?

ja.Where(Function(x) x("status").Value(Of String)="Checked").FirstOrDefault().Item("numberoflines").Value(Of String)

note: ja is JsonArray

Regards,

Hi @Luke69

JSONContent(“report”) is a JArray, so you must cast items to JObject and select the value.

Use this one:

JSONContent(“report”).
Where(Function(r) r(“status”).ToString = “Checked”).
Select(Function(r) r(“numberoflines”).ToString).
FirstOrDefault

This safely returns the numberoflines for status = “Checked” regardless of order.

It all sounds logical. You state it is an Array but so far I can tell the JSONContent is a JObject. It is defined as datatype JObject as response from an API call. Trying to read is as Datatype JArray doesn’t work as is states “Current JsonReader item is not an array”
So tried your, and Yoichi’s, options, but it keeps coming back with the same style error ((')'expected and disallow from string to boolean) which I cannot place in perspective.

@Luke69

Your JSONContent is a JObject, not a JArray.
Only JSONContent(“report”) is the JArray.

DirectCast(JSONContent(“report”), Newtonsoft.Json.Linq.JArray).
Where(Function(r) r(“status”).ToString = “Checked”).
Select(Function(r) r(“numberoflines”).ToString).
FirstOrDefault

It keeps bothering about
“…Where(Function(r) r(“status”).ToString()”.(2) : error BC30198: ‘)’ expected. (2) : error BC30512: Option Strict On disallows implicit conversions from ‘String’ to ‘Boolean’."
irrelevant of my first tries of the options you have given. As if it doesn’t recognise that the “= “Checked”” is part of the comparisson

@Luke69
This is a VB.NET parsing issue, not a JSON one

VB sometimes misreads the inline comparison in LINQ.

Use Equals instead of = and add parentheses:

DirectCast(JSONContent(“report”), Newtonsoft.Json.Linq.JArray) _
.Where(Function(r) r(“status”).ToString().Equals(“Checked”)) _
.Select(Function(r) r(“numberoflines”).ToString()) _
.FirstOrDefault()

Coffee? wine?
You are brilliant. That was the solution equals instead of =

1 Like

Now I could drop the direct cast as well.

1 Like

@Luke69

If this was helpful, kindly mark it as the solution.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.