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.
ppr
(Peter Preuss)
July 5, 2024, 9:06am
3
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
ppr
(Peter Preuss)
July 5, 2024, 9:12am
4
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”
ppr
(Peter Preuss)
July 5, 2024, 10:37am
5
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
SorenB
July 30, 2024, 12:14pm
6
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
system
(system)
Closed
August 2, 2024, 12:15pm
7
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.