Muhammad Waseem · Sep 24

How to get Specific element from JSON file by Calling API from objectscript?


Following API:
is returning following JSON response:
{"code":200,"status":"OK","data":{"hijri":{"date":"16-02-1443","format":"DD-MM-YYYY","day":"16","weekday":{"en":"Al Juma'a","ar":"\u0627\u0644\u062c\u0645\u0639\u0629"},"month":{"number":2,"en":"\u1e62afar","ar":"\u0635\u064e\u0641\u064e\u0631"},"year":"1443","designation":{"abbreviated":"AH","expanded":"Anno Hegirae"},"holidays":[]},"gregorian":{"date":"24-09-2021","format":"DD-MM-YYYY","day":"24","weekday":{"en":"Friday"},"month":{"number":9,"en":"September"},"year":"2021","designation":{"abbreviated":"AD","expanded":"Anno Domini"}}}}

How can I  get "hijri":{"date":"16-02-1443" portion from response JSON by calling above API by passing parameter date=24-09-2021 from objectscript?



Product version: IRIS 2021.1
1 0 8 234
Log in or sign up to continue

Hi Muhammad,

Create following class :

Class Test.Json

ClassMethod Get(date = "24-09-2021", debug = 0) As %String
  #Dim status as %Status
  #Dim response as %Stream
  #Dim jsonResponse as %DynamicObject Set objHttp = ##class(%Net.HttpRequest).%New()
  Set objHttp.Server=""
  Set objHttp.Https=0
  Do objHttp.SetParam("date",date)
  Set status = objHttp.Send("GET","v1/gToH")
  If status'=1 Do $SYSTEM.OBJ.DisplayError(status) Quit ""
  If objHttp.HttpResponse'="" Do
  Set response = objHttp.HttpResponse.Data
  Set jsonResponse = ..GetResponse(response)
  If debug Write,!

ClassMethod GetResponse(response As %Stream, del As %String = "") As %DynamicObject
  #Dim jsonStr as %String = ""
  If response'="" {
    Do response.Rewind()
    While 'response.AtEnd {
      Set jsonStr = jsonStr _ response.ReadLine() _ del
  Quit {}.%FromJSON(jsonStr)


and call it like :

USER>set hijri=##class(Test.Json).Get()
USER>write hijri
USER>set hijri=##class(Test.Json).Get(,1)
{"date":"16-02-1443","format":"DD-MM-YYYY","day":"16","weekday":{"en":"Al Juma'a","ar":"الجمعة"},"month":{"number":2,"en":"Ṣafar","ar":"صَفَر"},"year":"1443","designation":{"abbreviated":"AH","expanded":"Anno Hegirae"},"holidays":[]}
USER>Write ##class(Test.Json).Get("01-08-2021")

And if you just want a function to return hijri dates, you van try following build-in  $zdate function :

Write $zdate($zdateh("2021-09-24",3),19),!
16 2 1443

You can also try ,18 which gives 16 Safar 1443

Hi Muhammad,

Remove the 'Extends %Net.HttpRequest'  : it is not needed to inherit from this class.

Since the method Get is already defined in that class, it gives a compiler error when you overwrite a class with a different signature (classmethod).

assuming, the variable jsonStr contains the response from the API, then            

write jsonStr ---> {"code":200,"status":"OK","data":...
set object={}.%FromJSON( jsonStr )

That's all.

The '%FromJSON' class is only available in IRIS ?

%FromJSON is not a class, it's a method of %DynamicObject and is available in IRIS and for Cache  version 2016.2 and later.