Best ways to retrieve JSON objects from a feed?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Best ways to retrieve JSON objects from a feed?

Tim Mackinnon
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
Reply | Threaded
Open this post in threaded view
|

Re: Best ways to retrieve JSON objects from a feed?

Sven Van Caekenberghe-2
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 <[hidden email]> 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


Reply | Threaded
Open this post in threaded view
|

Re: Best ways to retrieve JSON objects from a feed?

Sven Van Caekenberghe-2


> On 20 Mar 2020, at 14:40, Sven Van Caekenberghe <[hidden email]> 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'.