access object in a JSON array.

JSON, REST API, Ensemble

Hi,

I am working with this Ensemble Business Operation, which extends the EnsLib.Rest.Operation.

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=...

I built the OpenWeatherMap Rest Operation described in the article above.

I would like to access the weather.id weather.main, and weather.description. However, these objects are in a JSON Array.

How do you access these three objects in Objectscript.

In my,  Test.REST.WeatherOperation class, I added these three lines.

            set pResponse.WeatherId = tProxy.weather.id
            set pResponse.WeatherMain = tProxy.weather.main
            set pResponse.WeatherDescription = tProxy.weather.description

Also, in my Test.REST.WeatherResponse, response message class I added these three lines.

Property WeatherId As %String;
Property WeatherMain As %String;
Property WeatherDescription As %String;

I get this error, with the code I have.

ERROR <Ens>ErrException: <PROPERTY DOES NOT EXIST>

Thanks in Advance.

  • 0
  • 0
  • 458
  • 2
  • 1

Answers

<PROPERTY DOES NOT EXIST> means your code is trying to reference a class property that does not exist.  The rest of the error message should tell you which property you tried to reference that didn't exist.

After you added those 3 properties to the WeatherResponse class, did you save and compile it?

If you're not able to resolve this, can you post the complete error message?  What property is it telling you doesn't exist?

I read your question more carefully.  Here is example JSON with an array:

{

    "test":"abc",

    "arr":["one","two","three"]

}

Here is how to access the contents of the array once it's loaded in the tProxy object:

USER>w tProxy.test
abc
USER>w tProxy.arr.GetAt(1)
one
USER>w tProxy.arr.GetAt(2)
two
USER>

 

Hi, I added the following code and it works.

            set pResponse.WeatherId = tProxy.weather.GetAt(1).id
            set pResponse.WeatherMain = tProxy.weather.GetAt(1).main
            set pResponse.WeatherDescription = tProxy.weather.GetAt(1).description

 

thanks,