API name/value pairs

Hello

I am getting results from an API in the structure shown below.

After deserializing JSON, how can I “grab” the various values by the names?
Lets say I wanted to get the value for name “trf_name” (which is “Rejsekort”).

Bear in mind that the name/value pairs could be more or fewer, and also in different order.

@SorenB

i hope it helps you

We can deserialize the JSON into JOBject - myJObject

from this we can create a datatable
dtData = myJObject("param").ToObject(Of DataTable)
and can filter / search in the datatable

OR
arrTRFNames | String Array =

myJObject("param").Values(Of JObject).Where(Function (x) x("name").Value(Of String).Trim.Equals("trf_name")).Select(Function (x) x("Value").Value(Of String)).ToArray

Also we can adapt and e.g. switch the LINQ to Query Syntax

in addition to above, when all name values are unique we can create a dictionary

deserialize the JSON into JOBject - myJObject

Assign Activity:
myLKDict | Dictionary(Of String, String)=

myJObject("param").Values(Of JObject).ToDictionary(Function (x) x("name").toString.Trim, Function (x) x("value").toString.Trim

myDict(“trf_name”) will return “Rejsekort”

when JSON Path would also be an option we can achieve with this a more compact filtering
https://goessner.net/articles/JsonPath/
Online Box:

Sample

last line demonstrates the empty result, which will result to an empty array, but not throwing an exception

I simply went with a “For each” and an If-sentence to check whether I am on the wanted name.

For each jsonobject("param")
If param("name") = "trf_name"
Assign str_Tarifname = param("value")

Regards

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