0xE2 is an invalid start of a property name - json

After installing a HAL Validator (https://paragsarin.medium.com/hal-api-validation-754fd3b2c96), and preparing a CURL request, I received the error "0xE2 is an invalid start of a property name". The response of my HAL service validates as JSON already, so why am I getting this error?

Turns out this error was from parsing a json based configuration file of the validator, not the results from validating a HAL response (which is also json). The json in the configuration file turned out to have left and right double quotes (disallowed) rather than standard double quotes. These characters came from copying from a web browser into an editor when creating the configuration file. Swapping left and right with standard double quotes fixed the issue.

For me, the issue was caused by toString method in my POST request:
httpRequest.body(requestParams.toString())
Taking off toString resolved the issue:
httpRequest.body(requestParams);

Related

Round-tripping JSON in PowerShell 5.1 results in HTTP error 400

I'm grabbing a JSON payload from a REST API which I intend to modify and then send back. I convert the payload to an object using ConvertFrom-Json, manipulate it using .Remove(), and convert it back to JSON with ConvertTo-Json.
When I send the modified payload back to the API, I get a "(400) Bad Request." Thinking I screwed something up when modifying the payload, I removed that part of the code but still got the same error, even though I'm "just" converting the unmodified payload from and to JSON.
Sending back an unmodified payload should result in a "(422) UNPROCESSABLE ENTITY" and if I send the payload back without even converting from and to JSON that's the response I get from the API.
In this case the -Depth parameter isn't the culprit as far as I can tell (I've added -Depth 100 just to be sure). I also output the payload before the ConvertFrom-Json and after the ConvertTo-Json operations, and the output looks identical on screen.
If I copy the output after the modifications and conversions from and to JSON and then paste that as the body in a Postman call - where all the other parameters are the same as in my code - the API call works as expected.
Checking the API logs on the server only contains this error: "BadRequest: 400 Bad Request: Failed to decode JSON object: 'utf8' codec can't decode byte 0xf6 in position 2976: invalid start byte"
This makes me think that the conversion changes the encoding of the string but I can't for the life of me figure out how that would be possible.
After a day and a half trying to get to the bottom of this I'm more than happy get any and all feedback on this problem. :)
Edit: This must work with PowerShell 5.1 as that's the only version I can safely assume is installed.
Thanks.
The solution to this was to use PowerShell 7.2. I spent almost 2 days trying to work around the limitations of JSON conversions in Windows PowerShell 5.1 to no avail.
In PowerShell 6.2+ you can add an -EscapeHandling variable to ConvertTo-Json with the value of EscapeNonAscii that will convert any non ASCII characters back to Unicode.
The downside to this is that I now have to ensure that there is PowerShell 6.2+ on the servers I remote into, but that's easier to control and manage than the results of the API call.

WSO2 Amazonsns connector issues publishing json message

I am trying to publish a json message using the Amazonsns connector in WSO2 ESB 4.9. I am able to successfully publish a simple string message however when I set the messagestructure to json in order to send different messages to different platforms and attempting to send json as the value of message it will not work. I am using a simple transaction that looks almost exactly like the documentation sample. My Transaction:
content-type: application/json;charset=UTF-8
{
"region":"us-west-2",
"accessKeyId":"MyAccessKey",
"secretAccessKey":"MySecretAccessKey",
"version":"",
"messageStructure":"json",
"subject":"Test",
"message": {"default":"mess","email":"message"},
"targetArn":"arn:aws:sns:us-west-2:977102061874:endpoint/APNS_SANDBOX/mobile_iOS_Sandbox/34ed4324e6-1119-67sd-b7dd-f413c88e4e25",
"topicArn":""
}
My result is an unexpected error sending message out.
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,73]Message: Illegal character: <d>
My service is also like the example:
<amazonsns.init>
<region>{$ctx:region}</region>
<accessKeyId>{$ctx:accessKeyId}</accessKeyId>
<secretAccessKey>{$ctx:secretAccessKey}</secretAccessKey>
<version>{$ctx:version}</version>
</amazonsns.init>
<amazonsns.publish>
<message>{$ctx:message}</message>
<subject>{$ctx:subject}</subject>
<messageStructure>{$ctx:messageStructure}</messageStructure>
<topicArn>{$ctx:topicArn}</topicArn>
<targetArn>{$ctx:targetArn}</targetArn>
</amazonsns.publish>
I did not expect much success but just started trying different things....I attempted xml encoding the json message and get the same message on '&'. Using wire logs I found the content type of the successful messages going to Amazon were application/x-www-form-urlencoded so I also tried url encoding and get same message on the '%'.
I'm running out of ideas. I double checked the connector documentation to make sure I did not miss anything. It does say you can use a JSON transaction for Publish and gives a sample transaction however it does not discuss anything special/different that you would need to set up in order to make this work. Is there something I am missing?
UPDATE: Can see it is using the Builder for application/x-www-form-urlencoded which is stumbling on the json. Have tried setting messageType and ContentType properties to application/json. JSON Builder is enabled. It just seems the connector wants to send it out as application/x-www-form-urlencoded, is there something I am not setting properly to tell the connector this is JSON?
UPDATE: The publish template that comes with the connector is setting the messagetype to applicaiton/x-www-form-urlencoded which is overriding any of the settings I am making in my proxy service. Going to look at changing the template in the connector to use a different messagetype and/or allow me to set it in the proxy dynamically then will be trying again.
Joe
We have created a public JIRA for this issue. Please follow that.
You need to enable message builders for the content types you are sending. make sure following is done.
Enable relevent Message builder for each content-type.
< messageBuilder contentType="application/json"
class="org.apache.synapse.commons.json.JsonStreamBuilder"/>
Make sure Content-Type header property is parsing with the message.
Make sure your Json is wellformed.

how to resolve json validation error in mlab?

I have this document which nicely uploads to robomongo but in mlab(mlab.com)it is showing JSON validation error.
Specifically ,"We encountered an error while parsing your JSON. Please check your syntax (e.g. ensure you are using double quotes around both your field names and values) and try again. " is making me nervous.
please check the document here.
That appears to be an array of JSON documents, not a single JSON document, which is what the mLab JSON document editor expects. In other words, an array is not a valid JSON document, even though its elements may be valid JSON documents.

AFNetworking received non-English character: how to convert?

I am getting JSON response from some web server, say the server returns:
"kən.grætju'leiʃən"
I use AFNetworking and JSONKit, but what I've received is:
"æm'biʃən"
Not sure if it's AFNetworking's problem or JSONKit's problem, but any way, how to I parse and convert the string so it looks the same as from server?
Thanks
The server may be returning characters encoded in a way that violates the official JSON spec. If those characters are encoded as escaped unicode IDs (like \U1234) then JSONKit and NSJSONSerialization should both handle them fine.
If you can't change the server, you can work around the issue by URL-decoding the string - see https://stackoverflow.com/a/10691541/1445366 for some code to handle it. But if your server isn't following the correct specs, you're likely to run into other issues.

Why does SJCL report "this is not JSON" when trying to decode this JSON snippet?

I'm using SJCL, and it works fine with small ASCII strings.
But when I try to decode this piece of JSON (the result of the encryption of an HTML page) I get a "this is not JSON!" error.
The JSON has been produced by SJCL, and while I did encode it and decode it using LZW and base64 I don't get this error for small strings with the same workflow.
I tracked the error message origin to the decode function. I assume the regexes are failing but I don't understand why as this seems to be a perfectly formed JSON string to me.
However, I can be wrong as if I do a JavaScript eval on it it fails on a syntax error. But if I dump it in a file Python parse it fine.
The json that is at your this piece of json link starts and ends with a double-quote character. Is that actually part of the contents of the json? If it is, I believe that is your problem. Otherwise, it looks like valid json to me.
Ok I made a double passed base64 encoding. One before encryption, and one after. It seems that removing the first pass make it work.