Question Jimmy Christian · Jul 16, 2019

Http Response Parsing JSON string or stream

Hello community,

I am trying to parse the below HttpResponse in Cache. Cannot get the Iterator to work. Is there a single loop which can parse both or single messages and grab the error?

{
error:[
{ txt1:'error msg1'},

{ txt2:'error msg2'},
]
}

 

{
error: {
txt1:[
'error msg1',

'error msg2'
]
}

 

Thanks,

Jimmy Christian

Comments

Jimmy Christian  Jul 16, 2019 to Eduard Lebedyuk

Cache for Windows (x86-64) 2018.1.1 (Build 312_1_18937U) Fri Apr 26 2019 17:58:36 EDT

I tried dynamic objects but gives me an error, unless i am not doing it correctly or if syntax is not correct.

Set objStream = objHttpResponse.Data
     Set json =""
     While ('objStream.AtEnd) {
          Set json = json _ objStream.ReadLine()
  
     }

set iter = json.%FromJSON().%GetIterator()
   while iter.%GetNext(.key , .value )
      {
       write "key = "_key_" , value = "_value,!
             }

0
Eduard Lebedyuk  Jul 16, 2019 to Jimmy Christian

Well, the JSON you posted is not a valid JSON. Where did you get it?

Valid JSON looks like this:

{
   "error":[
      {
         "txt1":"error msg1"
      },
      {
         "txt2":"error msg2"
      }
   ]
}
0
Eduard Lebedyuk  Jul 16, 2019 to Jimmy Christian

You can simplify your code to this:

set dynObj = {}.%FromJSON(objHttpResponse.Data)
set iter = dynObj.%GetIterator()
while iter.%GetNext(.key , .value )  {
       write "key = "_key_" , value = "_value,!

}

Also json is a string so in your code sample it won't have %FromJSON method.

0
Jimmy Christian  Jul 16, 2019 to Eduard Lebedyuk

Thank you Eduard.

So the message i posted is a response to an API call i make with JSON payload.

So i was assuming that the response would be JSON and i can try to parse it.

But if it is not a JSON, is there a utility to parse it or i need to use the provided STRING functions like ZSTRIP and so on?

Thanks,

Jimmy christian.

0
Eduard Lebedyuk  Jul 16, 2019 to Jimmy Christian

Can you make the same call to the API using Postman and show the response?

It's very strange that API returns non-json response.

Does my code snippet throw the same error?

0
Jimmy Christian  Jul 16, 2019 to Eduard Lebedyuk

The issue is the API  is not set up yet on the project which i am working.

But the specs has the JSON PAYLOAD as request and then the response is what i pasted above. 

So i am trying to get the code ready to parse in the meantime until the API is set up. Hopefully i will have something to work with POSTMAN as soon as API is set up.

Once i receive the response , i need to parse it for any error and then shutdown the operation.  At the most i can convert the response to string and check for error.

0
Eduard Lebedyuk  Jul 16, 2019 to Jimmy Christian

Well, maybe it's a handwritten response?

I'd recommend contacting the API developer and verifying that API responses would be indeed JSON.

0
Jimmy Christian  Jul 16, 2019 to Eduard Lebedyuk

Thank you. Yes, i am going to find out in a day or two.

Appreciate your help and time.

0
Neerav Verma  Jul 17, 2019 to Jimmy Christian

JSONStreamToObect should do it.

If you can just paste the exact json, I can write it for you. No biggy

You can use this site to verify if json is correct

http://jsonviewer.stack.hu/

0
Jimmy Christian  Jul 17, 2019 to Neerav Verma

Thank you Neerav.

The message i pasted doesnt look like  JSON. 

Looks like i may have to use some STRING functions to parse it. 

I will let you know once i get the right specs of the response. Thank you for time.

Regards,

Jimmy Christian.

0
Neerav Verma  Jul 17, 2019 to Jimmy Christian

No, it is JSON . Just one simple missing bracket. Here is the correct json for the same

{
error:[
{ txt1:'error msg1'},
{ txt2:'error msg2'},
],
error2:[
{ txt1:'error msg1'},
{ txt2:'error msg2'},
]
}

0
Eduard Lebedyuk  Jul 17, 2019 to Neerav Verma

How is it valid JSON?

1. JSON accepts only double quotes. Single quotes are not valid.

2. Property names must be quoted (in double quotes). error/txt are not quoted.

Here's JSON standard.

0
Neerav Verma  Jul 19, 2019 to Eduard Lebedyuk

Json parsers are parsing it. 

However I haven't tried to run it by code to see if it works.  

0
Vladimir Iliychev  Jul 19, 2019 to Eduard Lebedyuk

Yes, this works if you have one level. But what about if you have second level-something like this:

{
    "statuses": [
        "SW7=ON, SW6=OFF, SW5=OFF, SW4=OFF, SW3=OFF, SW2=OFF, SW1=OFF",
        "Unique Printer ID and Fiscal Memory ID are set",
        "The fiscal memory is formatted"
    ],
    "warnings": [],
    "errors": [],
    "ok": true
}

0