Retrieving element name from JSON Message

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
Eugene
Posts: 8
Joined: Sat Apr 22, 2017 12:01 am

Retrieving element name from JSON Message

Post by Eugene » Thu Jun 13, 2019 11:32 pm

Is there a way to retrieve the element name from a JSON message as well as the value?
I'm using the URL below to retrieve a list of the exchange rates. I now want to read through the returned JSON message and not only retrieve the value but also the element (key) name.

https://api.exchangeratesapi.io/latest

{
base: "EUR",
rates: {
BGN: 1.9558,
NZD: 1.7206,
ILS: 4.0544,
RUB: 73.0227,
CAD: 1.5002,
USD: 1.132,
},
date: "2019-06-11"
}

Thanks in advance.
Eugene

tsupartono
Posts: 74
Joined: Wed Jan 25, 2017 11:12 am

Re: Retrieving element name from JSON Message

Post by tsupartono » Fri Jun 14, 2019 4:38 pm

I'm assuming you are going to do this on your server-side code (server module).
The best way right now is to use PRIM_JSON.Reader
See https://apps.lansa.com/lansav14sp2/dem/xdemow_12.html for usage example.

Have a look at this forum post as well:
viewtopic.php?f=3&t=2046#p5374

While using PRIM_JSON.Reader is not the most straightforward thing to do, it would be the best solution for now until EPC142060 is released - after which you can use PRIM_JSON.Document, which is much easier to use (currently there are issues getting decimal values using PRIM_JSON.Document).

If this is just for demonstration, you can also do it client side using a widget.

soa
Posts: 271
Joined: Mon Dec 07, 2015 3:15 pm

Re: Retrieving element name from JSON Message

Post by soa » Fri Jun 21, 2019 9:56 am

We don't have EPC142060 installed as it is not released but in another post I saw these declarations

Define_Com Class(#PRIM_JSON.Document) Name(#lDocument)
Define_Com Class(#PRIM_JSON.Object) Name(#lRequestObject) Reference(*DYNAMIC)
Define_Com Class(#PRIM_JSON.Member) Name(#lRequestMember) Reference(*DYNAMIC)
Define_Com Class(#PRIM_JSON.Array) Name(#lLinks) Reference(*DYNAMIC)

These all seem to work (i.e. the IDE finds them) our current install (up to EPC14240). Are these classes functional at this EPC level. Can I start playing with them now in anticipation of 142060 or have they changed much. Do you have any example code?

tsupartono
Posts: 74
Joined: Wed Jan 25, 2017 11:12 am

Re: Retrieving element name from JSON Message

Post by tsupartono » Fri Jun 21, 2019 11:48 am

You can play around with it now, there won't be any change in the API itself.

The upcoming EPC142060 corrects some issues in PRIM_JSON.Document:
  • LoadFromString does not take unicode string
  • LoadFromFile missing path parameter
  • Getting decimal value throws an error
Also, if you use the publishing web services feature, the EPC after 142060 (EPC142070) introduces a new (better) way to read/write the request/response data, so you may not even need to use PRIM_JSON.Document at all.

soa
Posts: 271
Joined: Mon Dec 07, 2015 3:15 pm

Re: Retrieving element name from JSON Message

Post by soa » Fri Jun 21, 2019 12:11 pm

Thanks tony

Post Reply