Escape a hypen in JSON response from Mule - json

I am using Anypoint Studio 6.1 and Mule 3.8.1 and have the following JSON response from a Mule workflow:
{
"Description": "Top 10 games:
- Mario Kart
- Legend of Zelda"
}
This fails validation with the following error:
Error: Parse error on line 2:
{ "Description": "Top 10 games: - M
-----------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
I have tried escaping the special characters put it still does not work. How can I change the response to pass JSON v4 validation. The JSON validator I have been using is http://jsonlint.com/
Thanks

The root cause of the issue is carriage return in the JSON response. The valid JSON should be as below
{
"Description": "Top 10 games:\r\n- Mario Kart\r\n- Legend of Zelda"
}
Please provide more details on where you are receiving this description. is it from XML or any other source. Also it will be helpful if you post your DWL code as well.

Related

Parse JSON with missing fields using cjson Lua module in Openresty

I am trying to parse a json payload sent via a POST request to a NGINX/Openresty location. To do so, I combined Openresty's content_by_lua_block with its cjson module like this:
# other locations above
location /test {
content_by_lua_block {
ngx.req.read_body()
local data_string = ngx.req.get_body_data()
local cjson = require "cjson.safe"
local json = cjson.decode(data_string)
local endpoint_name = json['endpoint']['name']
local payload = json['payload']
local source_address = json['source_address']
local submit_date = json['submit_date']
ngx.say('Parsed')
}
}
Parsing sample data containing all required fields works as expected. A correct JSON object could look like this:
{
"payload": "the payload here",
"submit_date": "2018-08-17 16:31:51",
},
"endpoint": {
"name": "name of the endpoint here"
},
"source_address": "source address here",
}
However, a user might POST a differently formatted JSON object to the location. Assume a simple JSON document like
{
"username": "JohnDoe",
"password": "password123"
}
not containing the desired fields/keys.
According to the cjson module docs, using cjson (without its safe mode) will raise an error if invalid data is encountered. To prevent any errors being raised, I decided to use its safe mode by importing cjson.safe. This should return nil for invalid data and provide the error message instead of raising the error:
The cjson module will throw an error during JSON conversion if any invalid data is encountered. [...]
The cjson.safe module behaves identically to the cjson module, except when errors are encountered during JSON conversion. On error, the cjson_safe.encode and cjson_safe.decode functions will return nil followed by the error message.
However, I do not encounter any different error handling behavior in my case and the following traceback is shown in Openresty's error.log file:
2021/04/30 20:33:16 [error] 6176#6176: *176 lua entry thread aborted: runtime error: content_by_lua(samplesite:50):16: attempt to index field 'endpoint' (a nil value)
Which in turn results in an Internal Server Error:
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>openresty</center>
</body>
</html>
I think a workaround might be writing a dedicated function for parsing the JSON data and calling it with pcall() to catch any errors. However, this would make the safe mode kind of useless. What am I missing here?
Your “simple JSON document” is a valid JSON document. The error you are facing is not related to cjson, it's a standard Lua error:
resty -e 'local t = {foo = 1}; print(t["foo"]); print(t["foo"]["bar"])'
1
ERROR: (command line -e):1: attempt to index field 'foo' (a number value)
stack traceback:
...
“Safeness” of cjson.safe is about parsing of malformed documents:
cjson module raises an error:
resty -e 'print(require("cjson").decode("[1, 2, 3"))'
ERROR: (command line -e):1: Expected comma or array end but found T_END at character 9
stack traceback:
...
cjson.safe returns nil and an error message:
resty -e 'print(require("cjson.safe").decode("[1, 2, 3"))'
nilExpected comma or array end but found T_END at character 9

Unable to parse a valid Json from Arabic text

This appears to be valid Json but Json parsers throw an exception on address text. Can we not have arabic values as Json values?
{
"deliveryTime":"12:34:00",
"deliveryDate":"07-09-2017",
"paymentType":1,
"deliveryMethod":1,
"address":{
"text":"\"7774 جبل الجزيره، طويق"\, "\الرياض \13791 3836، السعودية\"",
"geo":{
"latitude":24.580577,
"longitude":46.518820
},
"noteToDriver":"Near bank",
"country":"Saudi Arabia"
}
}
https://jsonlint.com/ throws an exception but I couldn't figure out the issue. It has double quotes properly escaped, the entire address value is quoted. I think the problem is due to the right to left nature of the language that resulted in the error.
Error: Parse error on line 7:
...4 جبل الجزيره، طويق"\, "\الرياض \13791
-----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'undefined'

Invalid JSON - Cannot see why

The following JSON string comes back as invalid in several formatter tests. I cannot figure out for the life of me what is wrong!
{
"draw": 122,
"recordsTotal": 1496,
"recordsFiltered": 1496,
"data": [["11315","1403","John Doe","Parking Pass","-","Hyundai Sonata - Grey ABC 123 ","09-01-2016 2:00 AM","09-01-2016 7:00 AM","-"]]
}
https://jsonformatter.org/ is saying:
Parse error on line 5: ...,"Parking Pass","-","Hyundai Sonata - Gr
-----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
https://jsonformatter.curiousconcept.com/ says:
Error:Invalid characters found.[Code 18, Structure 28]
and highlights the line "Hyundai Sonata - Grey ABC 123 ",
What the heck am I missing? I dont see any invalid characters anywhere. If I paste it into Notepad++ its not showing anything but "CRLF" on the line breaks.
After checking, it turns out that the value, which was being returned from a database, had a tab in it. I created a function to strip out any non ASCII chars and its working fine now

Grails 3.2.9: Spring Rest Docs using Rest Assured giving Payload Handling Exception: com.fasterxml.jackson.core.JSONParseException

I am trying to integrate Spring Rest docs with Grails 3.2.9 application, using Rest Assured. During integration testing it is producing the Payload Handling Exception.
Full stack trace:
org.springframework.restdocs.payload.PayloadHandlingException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: [B#793cd42d; line: 1, column: 2]
at org.springframework.restdocs.payload.JsonContentHandler.readContent(JsonContentHandler.java:84)
at org.springframework.restdocs.payload.JsonContentHandler.findMissingFields(JsonContentHandler.java:50)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:113)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:74)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:192)
at org.springframework.restdocs.restassured.RestDocumentationFilter.filter(RestDocumentationFilter.java:63)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer.filter(RestAssuredRestDocumentationConfigurer.java:65)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1574)
at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:159)
at so.con.BaseRestDocTestSetup.$tt__createDoc(BaseRestDocTestSetup.groovy:130)
at so.con.BaseRestDocTestSetup.createDoc_closure4(BaseRestDocTestSetup.groovy)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
at so.con.v1.docs.ConfApiDocSpec.test and document get request for /conf getting conf-list as Response(ConfApiDocSpec.groovy:63)
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: [B#793cd42d; line: 1, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:558)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:456)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2689)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:878)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:772)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3834)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3783)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2929)
at org.springframework.restdocs.payload.JsonContentHandler.readContent(JsonContentHandler.java:81)
... 19 more
Please help me resolve this.
You are trying to parse XML with JSON decoder. Use Accept-Encoding header for you request and provide expected format there.

malformed JSON from Google directions API when calling from R

This might be a duplicate- but I couldn't find something to solve this problem.
I'm using httpGET() to call the google directions API.
Packages:
require(RCurl)
require(rjson)
require(gooJSON)
the code is:
url = "http://maps.googleapis.com/maps/api/directions/json?origin=12.9673293,77.7173975&destination=12.9373613,77.700985&waypoints=optimize:true|12.9723379,77.7117611|12.9922162,77.715895|12.9629354,77.7122996&sensor=false"
routeJSON = httpGET(url= url)
routeList = fromJSON(routeJSON)
I get:
Error in fromJSON(routeJSON) :
unexpected escaped character '\]' at pos 18
I wrote the JSON to a file and copied it to jsoneditoronline.com. I got:
Error: Parse error on line 51:
... "points" : "qscnA_djyMj#kAT[\]\
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
validated by jsonlint
But it works when I put the URL into the browser and copy the output to jsoneditoronline.
Any idea why it is happening and/or how to circumvent it?
EDIT: I tried gooJSON, but it seems it does not support the maps API V3.
> goomap(url)
$Status
$Status$code
[1] 610
$Status$request
[1] "geocode"
$Status$error_message
[1] "The Geocoding API v2 has been turned down on September 9th, 2013. The Geocoding API v3 should be used now. Learn more at https://developers.google.com/maps/documentation/geocoding/"
The following works fine for me:
require(rjson)
url = "http://maps.googleapis.com/maps/api/directions/json?origin=12.9673293,77.7173975&destination=12.9373613,77.700985&waypoints=optimize:true|12.9723379,77.7117611|12.9922162,77.715895|12.9629354,77.7122996&sensor=false"
fromJSON(file=url)
However, if there is invalid json data, one can call
fromJSON(url, unexpected.escape="keep")
to to treat the escaped character as normal character (e.g. \] becomes ])