Linq Query To get data from json

Hello Team,

I am working on json which i have pasted below. I am able to get the data for Rule ID and default error using for each but the process speed is slow.

I am looking for a Linq query to get the required data from the json.

My requirement is, 'if i provide “Rule ID” value the required output is the value in “DefaultErrorLevel” ;Please look into below image

Capture

Thanks in advance

@ppr

hi

do you want to extract the rule id from json?

please more details to help you

@RK_0
If you need to extract Rule ID use regex

System.Text.RegularExpressions.Regex.Match(jsonString,"(?<=\WRuleId\W:\s\W).*(?=\W,)").ToString

And also try this

jsonObject("Rules")("RulesId").Tostring

if i provide the rule id, i need get the relative defaulterror value as output

im looking for linq query

thanks for response

@RK_0
Kindly share your input as a text file, its help us to provide you better solution

Sound like filtering. Give a try at:
myResult | List(Of JObject)

YourDeserializedJSONObjectVar("Rules").Values(Of JObject).Where(Function (x) x("RuleId").Value(Of String).Equals("001")).toList

then depending on the return result: 0, 1 possible also more JObjects, we can retrieve the DeffaultErrorLevel

Some additional illustrations with similar test data:

So we prefer to filter for the Rule JObject and deciding then, when existing after filtering to use the retrieval for the DefaultErrorLevel

We can do also:
grafik

But when Rule JO is not present or DefaultErrorLevel is not set, then we came into a scenario that we less know what is mising the JO or the property or the value

Kindly note:

  • grafik
    is an option to balance the scenario and the filter. We do not expect multiple rules with the same ID. So its more the case about Rule JObject found or not. Which we can handle with the FirstOrDefault

  • The code snippets from above can also be sliced and all parts can be used within assign statements

Also have a look on the the SelectTokens Method where we also can provide a filter in the statement

1 Like

Hello, I made a script for the Invoke Code activity.
arg1 is the text of the file read with Read Text File
arg2 is the “RuleId” argument and value is the result String
Being a particular JSON file the format can be unrecognized.
So better use no encoding instead of
“typescript/text”, or “application/json”
An “UTF-8” type encoding can be used as well.
Unfortunately, reading the file from the disk is very slow.
The code for this script is the following:

Dim doc As System.Text.Json.JsonDocument
doc = System.Text.Json.JsonDocument.Parse(arg1)

Dim id As String
Dim rules As System.Text.Json.JsonElement
rules = doc.RootElement.GetProperty(“Rules”)

Dim n As Integer = rules.GetArrayLength()

For i = 0 To n -1 Step 1
id = rules.Item(i).GetProperty(“RuleId”).GetString()
If (id = arg2) Then
value = rules.Item(i).GetProperty(“DefaultErrorLevel”).GetString()
''System.Windows.Forms.MessageBox.Show(value)
End If
Next i

Hope it will help somehow, Adrian

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