Web Connection
JSON validator functionality
Gravatar is a globally recognized avatar based on your email address. JSON validator functionality
  Richard Kaye
  All
  Apr 7, 2020 @ 08:54am

Hi Rick,

Now that I've got past my posting issue I'm running into another one and wondering if there's anything in the framework that can give me a pre-flight check to validate generated JSON? Unfortunately, the provider I'm working with can only tell me something is wrong and not where in the post data the problem(s) can be found.

Thanks.

Gravatar is a globally recognized avatar based on your email address. re: JSON validator functionality
  Rick Strahl
  Richard Kaye
  Apr 7, 2020 @ 03:29pm

Are you generating the JSON with wwJsonSerializer (or one of the service tools that use it) and you're running into problems that the JSON is invalid?

I'd be surprised by that! I use the JSON features in wconnect extensively for many API calls of all kinds and I've never heard of invalid JSON.

I'd suspect maybe that the data you're sending is not what they are expecting exactly. Perhaps casing of property names etc. isn't inline with their expectation. There's no JSON scheme validation so if it fails the server API is responsible for letting you know what doesn't work.

+++ RIck ---

Gravatar is a globally recognized avatar based on your email address. re: JSON validator functionality
  Richard Kaye
  Rick Strahl
  Apr 7, 2020 @ 03:42pm

And your surprise would be completely justified! 😃

I ran the post data through several different online validators, and it all passes. So there's nothing wrong with the formatting of the VKPs. After much mucking around (1.2MB of post data to fiddle with) I found that at least one of the items has the copyright symbol in the text. For some reason, this is being flagged as:

{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":5,"json_error_message":"Malformed UTF-8 characters, possibly incorrectly encoded"}}

Sadly, that's all I get back so just figuring out that particular use case was a giant PITA. We'll what the other devs come back with.

Gravatar is a globally recognized avatar based on your email address. re: JSON validator functionality
  Rick Strahl
  Richard Kaye
  Apr 7, 2020 @ 06:28pm

You can try UTF-8 encoding the JSON text when it's sent to the server with STRCONV().

Thinking about this this should probably happen automatically since UTF-8 is the recommended formatting anyway.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: JSON validator functionality
  Richard Kaye
  Rick Strahl
  Apr 8, 2020 @ 08:14pm

So something like:

.oHttp.AddPostKey(STRCONV(m.lcJson,...)

Gravatar is a globally recognized avatar based on your email address. re: JSON validator functionality
  Rick Strahl
  Richard Kaye
  Apr 9, 2020 @ 12:26pm

Yup...

If you're sending the JSON yourself then you definitely need to do this. If it's wwJsonServiceClient then it should do this automatically (I have to check and see if I already do or - don't think so).

Update

Yup, checked and the code in wwJsonServiceClient::CallService does not UTF-8 encode the outbound JSON. Changed the code to do that:

*** Serialize single value/object
IF !IsNullOrEmpty(lvPostData)
    loSer = THIS.CreateSerializer()
    lcJson = loSer.Serialize(lvPostData)
    loHttp.cContentType = "application/json; charset=utf-8"	
    loHttp.AddPostKey(STRCONV(lcJson,9))
ENDIF
Gravatar is a globally recognized avatar based on your email address. re: JSON validator functionality
  Richard Kaye
  Rick Strahl
  Apr 10, 2020 @ 04:11pm

Yes, that's basically what I added to my AddPostKey. I am using the standard wwHttp object Post method instead of CallService because that was somehow the root cause of the multiple escape sequence issue. I really have no idea why it was doing that and once I got past that roadblock buy using Post I haven't looked back. Still waiting on the endpoint provider to give me some useful responses for exceptions. Thanks again for your help.

© 1996-2024