Question
Kurro Lopez · Feb 25, 2021

Export partial JSon to a response message

Hi all,

I have a API operation that is calling to a external process. The answer is a big json but I only want a pice of the full content.

My first attempt is:

.....
// The code of prepare the request is omitted.
set tSC = ..Adapter.SendFormDataArray(.tHttpResponse,"POST",tHttpRequest,,,tURL)
if $$$ISERR(tSC) $$$ThrowStatus(tSC)
// Get the response directly
set response = ""
while (tHttpResponse.Data.AtEnd = 0) {
    set response = respuesta_tHttpResponse.Data.Read()
}
// Convert Json to generic object, get the node returnValue.data and convert to JSon again
set objJson = ##class(%DynamicObject).%FromJSON(response)
set jsonReturnValue = objJson.returnValue.data.%ToJSON()
$$$TRACE("Content ReturnValue: "_jsonReturnValue)
do ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(jsonReturnValue,"MyMsg.PartialJson",.pResponse)
......

The JSon response is:

{
  "result": "OK",
  "returnValue": {
    "code": "0",
    "message": "Info",
    "data": {
      "ext_customer_id": "123456789",
      "customer_name": "FRANCISCO",
      "customer_surname_1": "LÓPEZ",
      "customer_surname_2": "DE LAS HERAS",
      "passport_id": "123456ZZZ",
      "dob": "1972-02-01",
      "sex": "M",
      "email": "kurro.lopez@gmail.com",
      "active": true
    }
  }
}

and MyMsg.PartialJson class is:

Class MyMsg.PartialJson Extends Ens.Response
{
/// Id del usuario
Property "ext_customer_id" As %String(MAXLEN = 1024);
/// Name customer
Property "customer_name" As %String(MAXLEN = 1024);
/// Surname customer
Property "customer_surname_1" As %String(MAXLEN = 1024);
/// surname 2 customer
Property "customer_surname_2" As %String(MAXLEN = 1024);
/// Passport ID
Property "passport_id" As %String(MAXLEN = 250);
/// Date of birth
Property dob As %String(MAXLEN = 20);
/// Sex
Property sex As %String(MAXLEN = 10);
/// Email
Property email As %String(MAXLEN = 1024);
/// Check if customer is active
Property active As %Boolean;
}

But the pResponse object is empty.

is there any error?

Best regards
Kurro Lopez

 

 

 

Product version: Ensemble 2017.1
$ZV: Cache for Windows (x86-64) 2017.2.1 (Build 801_3U) Thu Apr 12 2018 10:02:23 EDT
00
1 0 2 66
Log in or sign up to continue

Replies

I'm not sure you can directly map from the JSON to response class in 2017. I think in the new Intersystems you can  here do this direct mapping where you add it into your message class how it maps. It's not available in 2017 as far as I'm aware. 

Id go for something like after you have the JSON Object 

set objJson = ##class(%DynamicObject).%FromJSON(response)

Set pResponse = ##class(MyMsg.PartialJson).%New()

set pResponse.ext_customer_id=objJson .ext_customer_id

...

Thanks Mark,

Finally I've used that solution.

Best regards,
Kurro