Login  Register

Best ways to retrieve JSON objects from a feed?

Posted by Tim Mackinnon on Mar 20, 2020; 1:14pm
URL: https://forum.world.st/Best-ways-to-retrieve-JSON-objects-from-a-feed-tp5113578.html

Hi everyone, I’m finally finding some space from world chaos and looking more into Pharo, and I have a question about retrieving json data from feeds - how is the best way to elegantly parse it - e.g.

I am doing :

url := ZnUrl fromString: 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr’.
url addPathSegments: {aDate1 asString. aDate2 asString. aCountryCode}.
 result := ZnEasy get: url.
 content := STONJSON fromString: result contents.
...

However I was kind of surprised there wasn't something on a ZnResponse for getting json objects e.g.
  result getJsonObjects

or something like that?

It’s so common I wonder if I am missing some trick? What aren’t there helpers in place for this or is there a better way to do this that I haven’t found yet?


I was kind of thinking something like (which obviously I can add myself)

result asJsonObjects sum: [:obj | (obj at: ‘annualData’) first ]

Tim