GET JSON STRANGE RESULT

Posted by Giancarlo Alberto Somma on 30-Dec-2019 06:34

Hi to ALL,

Follow the code for GET JSON clients data. Its seems all corret but the result is very strange.

The entity.json file contains this string: 'Progress.Json.ObjectModel.JsonArray_5057' insteand to receive json format file. Maybe could you explain me why?

Many thanks

BLOCK-LEVEL ON ERROR UNDO, THROW.

USING Progress.Json.ObjectModel.JsonObject.
USING Progress.Json.ObjectModel.ObjectModelParser.
USING Progress.Lang.Object.
USING OpenEdge.Core.WidgetHandle.
USING OpenEdge.Core.String.
USING OpenEdge.Core.*.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.RequestBuilder.
USING OpenEdge.Net.HTTP.Credentials.

DEFINE VARIABLE httpUrl AS CHARACTER NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oRequestBody AS String NO-UNDO.
DEFINE VARIABLE oJsonEntity AS JsonObject NO-UNDO.
DEFINE VARIABLE JsonString AS LONGCHAR NO-UNDO.
DEFINE VARIABLE lcToken AS LONGCHAR NO-UNDO.
DEFINE VARIABLE cToken AS CHARACTER NO-UNDO.
DEFINE VARIABLE oEntity AS Object NO-UNDO.
DEFINE VARIABLE hXmlDoc AS HANDLE NO-UNDO.
DEFINE VARIABLE lcHTML AS LONGCHAR NO-UNDO.

SESSION:DEBUG-ALERT = TRUE.

oRequestBody = new String('username=sage&password=$££££$$$&grant_type=password').

oRequest = RequestBuilder:Post("http://sage.ch/api/v1/token", oRequestBody)
:ContentType('application/x-www-form-urlencoded')
:AcceptJson()
:Request.

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

MESSAGE
oResponse:StatusCode SKIP
oResponse:StatusReason SKIP
VIEW-AS ALERT-BOX.

oJsonEntity = CAST(oResponse:Entity, JsonObject).
oJsonEntity:Write(JsonString, TRUE).

lcToken = oJsonEntity:GetJsonText("access_token").
cToken = oJsonEntity:GetJsonText("access_token").

MESSAGE STRING(JsonString) STRING(lcToken)
VIEW-AS ALERT-BOX.
//chToken = "Bearer" + STRING(lcToken).
oRequest = RequestBuilder:Get("sage.ch/.../getclients")
// Add credentials to the request
//:UsingBasicAuthentication(oCredentials)
:AddHeader("Authorization","Bearer " + cToken)
:Request.

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

MESSAGE
oResponse:StatusCode SKIP
oResponse:StatusReason SKIP
oResponse:ContentLength SKIP
oResponse:ContentType skip
VIEW-AS ALERT-BOX.

oEntity = oResponse:Entity.

IF TYPE-OF(oEntity, JsonObject) THEN
CAST(oEntity, JsonObject):WriteFile('c:\ob1\temp\entity.json', true).
ELSE
IF TYPE-OF(oEntity, WidgetHandle) THEN
DO:
hXmlDoc = CAST(oEntity, WidgetHandle):Value.
hXmlDoc:save('file', 'temp\entity.xml').
END.

ELSE
DO:
/*IF TYPE-OF(oEntity, String) THEN
lcHTML = CAST(oEntity, String):Value.
ELSE
lcHTML = oEntity:ToString().*/

case true:
when type-of(oEntity, String) then
lcHTML = cast(oEntity, String):Value.

when type-of(oEntity, Memptr) then
lcHTML = cast(oEntity, Memptr):GetString(1).

when type-of(oEntity, ByteBucket) then
lcHTML = cast(oEntity, ByteBucket):GetString().

otherwise
lcHTML = oEntity:ToString().
end case.

if OpenEdge.Net.MimeTypeHelper:IsXML(oResponse:ContentType) then
copy-lob lcHTML to file 'temp/entity.xml'.

else if OpenEdge.Net.MimeTypeHelper:Isjson(oResponse:ContentType) then
copy-lob lcHTML to file 'temp/entity.json'.

else copy-lob lcHTML to file 'temp/entity.txt'.

/* Change extension per the Response's ContentType
CASE oResponse:ContentType:
WHEN 'application/json' THEN
COPY-LOB lcHTML TO FILE 'temp/entity.json'.
WHEN 'text/html' THEN
COPY-LOB lcHTML TO FILE 'temp/entity.html'.
WHEN 'application/atom+xml' THEN
COPY-LOB lcHTML TO FILE 'temp/entity.xml'.
OTHERWISE
COPY-LOB lcHTML TO FILE 'temp/entity.txt'.
END CASE. */
END.

All Replies

Posted by Mike Fechner on 30-Dec-2019 07:08
Your response seems to contain a JSON Array – not a JSON Object.
 
You don’t handle the JsonArray type in your code. So somewhere your code must output oEntity:ToString() or STRING(oEntity).
 
Try this here:
 
IF TYPE-OF(oEntity, JsonConstruct) THEN
CAST(oEntity, JsonConstruct):WriteFile('c:\ob1\temp\entity.json', true).
 
JsonConstruct is the super-type of JsonArray and JsonObject.
Posted by goo on 30-Dec-2019 07:16
It’s a jsonArray, not a jsonobject, and if you try to use ToString() you Get that naming....

Try TypeOf(Entity,JsonArray) 

Sendt fra min iPad

30. des. 2019 kl. 07:36 skrev Giancarlo Alberto Somma <bounce-obonelinux@community.progress.com>:


<ProgressEmailLogo-png_2D00_150x42x2-png>
Update from Progress Community
<4U4AJMTEJRC6-jpg_2D00_70x70x2-jpg>
Giancarlo Alberto Somma

Hi to ALL,

Follow the code for GET JSON clients data. Its seems all corret but the result is very strange.

The entity.json file contains this string: 'Progress.Json.ObjectModel.JsonArray_5057' insteand to receive json format file. Maybe could you explain me why?

Many thanks

BLOCK-LEVEL ON ERROR UNDO, THROW.

USING Progress.Json.ObjectModel.JsonObject.
USING Progress.Json.ObjectModel.ObjectModelParser.
USING Progress.Lang.Object.
USING OpenEdge.Core.WidgetHandle.
USING OpenEdge.Core.String.
USING OpenEdge.Core.*.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.RequestBuilder.
USING OpenEdge.Net.HTTP.Credentials.

DEFINE VARIABLE httpUrl AS CHARACTER NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oRequestBody AS String NO-UNDO.
DEFINE VARIABLE oJsonEntity AS JsonObject NO-UNDO.
DEFINE VARIABLE JsonString AS LONGCHAR NO-UNDO.
DEFINE VARIABLE lcToken AS LONGCHAR NO-UNDO.
DEFINE VARIABLE cToken AS CHARACTER NO-UNDO.
DEFINE VARIABLE oEntity AS Object NO-UNDO.
DEFINE VARIABLE hXmlDoc AS HANDLE NO-UNDO.
DEFINE VARIABLE lcHTML AS LONGCHAR NO-UNDO.

SESSION:DEBUG-ALERT = TRUE.

oRequestBody = new String('username=sage&password=$££££$$$&grant_type=password').

oRequest = RequestBuilder:Post("http://sage.ch/api/v1/token", oRequestBody)
:ContentType('application/x-www-form-urlencoded')
:AcceptJson()
:Request.

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

MESSAGE
oResponse:StatusCode SKIP
oResponse:StatusReason SKIP
VIEW-AS ALERT-BOX.

oJsonEntity = CAST(oResponse:Entity, JsonObject).
oJsonEntity:Write(JsonString, TRUE).

lcToken = oJsonEntity:GetJsonText("access_token").
cToken = oJsonEntity:GetJsonText("access_token").

MESSAGE STRING(JsonString) STRING(lcToken)
VIEW-AS ALERT-BOX.
//chToken = "Bearer" + STRING(lcToken).
oRequest = RequestBuilder:Get("sage.ch/.../getclients")
// Add credentials to the request
//:UsingBasicAuthentication(oCredentials)
:AddHeader("Authorization","Bearer " + cToken)
:Request.

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

MESSAGE
oResponse:StatusCode SKIP
oResponse:StatusReason SKIP
oResponse:ContentLength SKIP
oResponse:ContentType skip
VIEW-AS ALERT-BOX.

oEntity = oResponse:Entity.

IF TYPE-OF(oEntity, JsonObject) THEN
CAST(oEntity, JsonObject):WriteFile('c:\ob1\temp\entity.json', true).
ELSE
IF TYPE-OF(oEntity, WidgetHandle) THEN
DO:
hXmlDoc = CAST(oEntity, WidgetHandle):Value.
hXmlDoc:save('file', 'temp\entity.xml').
END.

ELSE
DO:
/*IF TYPE-OF(oEntity, String) THEN
lcHTML = CAST(oEntity, String):Value.
ELSE
lcHTML = oEntity:ToString().*/

case true:
when type-of(oEntity, String) then
lcHTML = cast(oEntity, String):Value.

when type-of(oEntity, Memptr) then
lcHTML = cast(oEntity, Memptr):GetString(1).

when type-of(oEntity, ByteBucket) then
lcHTML = cast(oEntity, ByteBucket):GetString().

otherwise
lcHTML = oEntity:ToString().
end case.

if OpenEdge.Net.MimeTypeHelper:IsXML(oResponse:ContentType) then
copy-lob lcHTML to file 'temp/entity.xml'.

else if OpenEdge.Net.MimeTypeHelper:Isjson(oResponse:ContentType) then
copy-lob lcHTML to file 'temp/entity.json'.

else copy-lob lcHTML to file 'temp/entity.txt'.

/* Change extension per the Response's ContentType
CASE oResponse:ContentType:
WHEN 'application/json' THEN
COPY-LOB lcHTML TO FILE 'temp/entity.json'.
WHEN 'text/html' THEN
COPY-LOB lcHTML TO FILE 'temp/entity.html'.
WHEN 'application/atom+xml' THEN
COPY-LOB lcHTML TO FILE 'temp/entity.xml'.
OTHERWISE
COPY-LOB lcHTML TO FILE 'temp/entity.txt'.
END CASE. */
END.

View online

 

You received this notification because you subscribed to the forum.  To stop receiving updates from only this thread, go here.

Flag this post as spam/abuse.

Posted by Giancarlo Alberto Somma on 30-Dec-2019 09:05

Hi Mike,

that's works fine.

many  thanks.

This thread is closed