777
Views
5
Comments
[REST] Consume list of object where key is dynamic
Question
Hi,

I want to consume a REST-service with the following JSON

"measures": {
        "02:00:00:00:5b:be": 
	{
	 "res": {"1382003013": [19.6,70]},
         "type": ["temperature","humidity"]
        },
        "70:ee:50:00:58:82": 
	{
          "res": {"1382003056": [1021.9]},
           "type": ["pressure"]
        } 
      } 
as you can see. the "02:00:00:00:5b:be" is the key. is this possible with Outsystems, or should I fall back to ardoJSON/HTTP? so I can parse the raw JSON?
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
You can parse the raw JSON by specifying an OnAfterResponse, iirc. That'll still mean some work, but it's better than raw HTTP.
2012-03-16 12-21-09
João Rosado
Staff
Hi J,


Usually he recommendation we give on those scenarios is to transform the dynamic object in an array on OnAfterResponse with a regex .....but in your case with inner objects it's tricky to just to a regex.

Check the attached espace as example of the Regex search/replaces necessary for your example.
Note: my regex are sensible to the number of "{" inside the measure element... it's made specific for the example you posted.

Try it out online here: https://my.outsystemscloud.com/Demos/HomePage.aspx

It transforms each   "02:00:00:00:5b:be": {....}  pair into a  { "key" : "02:00:00:00:5b:be", "value" : { .... }



Note 2: tested in my personal only (C#) the first regex uses a lookbehind that is not "zero-length" ..and I'm not sure if Java supports it. Tell me if you are in java and I'll upload a version without that lookbehind (but needs an extra Regex_Replace ...)

Regards,
João Rosado
Demos2.oml
2016-04-21 20-09-55
J.
 
MVP
I'm in c# so that's fine :)

2014-02-13 10-06-38
Ricardo Silva
Hello,

I noticed this use-case and added this functionality to ardoJSON.

No need for weird and very specific regular expressions which may not work if the format is slightly different.

JSON_Lisfity will take up any object in a path and convert it into a list of key/value pairs. For example, your JSON would have become:

"measures": [
	{
           "key": "02:00:00:00:5b:be",
           "value": {
	       "res": {"1382003013": [19.6,70]},
               "type": ["temperature","humidity"]
           }
	},
	{
           "key": "70:ee:50:00:58:82",
           "value": {
               "res": {"1382003056": [1021.9]},
               "type": ["pressure"]
           }
      }
]
2016-04-21 20-09-55
J.
 
MVP
awesomesauce.
will check it out later!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.