Best ways to retrieve JSON objects from a feed?
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
Hi Tim, Sure this is possible (with the latest Zn of course): ZnClient new forJsonREST; url: 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr'; addPath: { '20190101'. '20191231'. #bel }; get. I do not know how to use this API to get actual data, but the above succeeds with an empty JSON result. If you study #forJsonREST you should be able to figure out how this is done. Here is another example (from the unit tests): ZnClient new forJsonREST; url: 'http://easy.t3-platform.net/rest/geo-ip'; queryAt: 'address' put: '81.83.7.35'; get. And a very simple one: ZnClient new forJsonREST; get: 'https://api.ipify.org?format=json'. Note that the result depends a bit on whether NeoJSON is loaded or not (the fallback is to use STONJSON which is always present in the image). HTH, Sven
On 20 Mar 2020, at 14:14, Tim Mackinnon <tim@testit.works> wrote:
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
On 20 Mar 2020, at 14:40, Sven Van Caekenberghe <sven@stfx.eu> wrote:
And a very simple one:
ZnClient new forJsonREST; get: 'https://api.ipify.org?format=json'.
Here is another cool example: ZnClient new forJsonREST; get: 'https://ip.seeip.org/geoip'.
participants (2)
-
Sven Van Caekenberghe -
Tim Mackinnon