I am reading an application.yml file in my java Spring application, and getting this property called body to send in a request (it is a very very long json), sometimes it contains names or values like you will see in the example ahead, and it messes up the yml, any way to solve this so that it takes the json properly? Here is a little example of the kind of data that messes my application.yml (that comes inside that very big json):
data:
body: '{"name":"O'brien"}'
The problem is the ' in the persons name
I tried using: <%='putting the very big json here'%> but then I get "Nested mappings are not allowed in compact mappings", also tried
<%=very big json%> but get the same error
Related
I am trying to write code using RestTemplate exchange to query an API that returns song info. The problem is that the data looks like this when data is found but
{"search":[{"song_id":"4R9o2J","song_title":"xxxx", ...}]
but like this if the song is not found
{"search":{"error":"no result"}}
When reading valid data my result object looks like this
Data
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(JsonInclude.Include.NON_NULL)
public class SongSearch {
List<SongInfo> search;
}
The problem is that when the data is valid, there is no property to contain the array of info... it is the contents of the "search": [{....}] so I can't create a property that will get ignored when it is not found even though the not found message has an error property. In other words, the search property can contain an array OR a single property depending on whether data was found or not.
My exchange call looks like this:
ResponseEntity<SongSearch> res = restTemplate.exchange(url, HttpMethod.GET, entity, SongSearch.class);
I've been able to write code that catches the deserialization exception in the not found case but the exception does not contain the actual error message from the API, which I'd want to return. I am currently able to hack it by making a call again with a different object to capture the error but that seems insane. There MUST BE a better way to do this. Just to be clear, this is not an actual error status returned from the client... the data is just different but it breaks the deserialization.
Anyone have suggestions?
I have a spring boot app configured with a RabbitMqListener. It has to receive JSON data of the format below: (showing sample)
{ "name" :"abc",
"key" : "somekey",
"value" : {"data": {"notes": "**foo \u0026 bar"}}**
}
This data represents some info. which should be used only for read-only processing, and the receiver spring app should receive it as it is(raw form).
What I mean is if I assert value node in spring app with input that was published on queue it should be equal.
This is simply not happening.
I always get the value in spring app as
foo & bar but I wanted it in raw form without a conversation of \u codes.
I try several approaches,
Jackaon2JsonMessage converter,
passing bytes from Message.getBody() - byte[] to mapper.readValue() in Rabbit handler.
Using JSON-simple, gson libraries
Why is it so tricky to get the data as it is, without any conversion or translation.
Do I need to follow an alternative approach?
Any help is appreciated
Have you tried explicitly enabling the escaping of non-ascii characters on your ObjectMapper?
mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
I'm trying to create an MUnit test that mocks an HTTP request by setting the payload to a JSON object that I have saved in a file. In Mule 3 I would have just done getResource('fileName.json').asString() and that worked just fine. In Mule 4 though, I can't statically call getResource.
I found a forum post on the Mulesoft forums that suggested I use MunitTools::getResourceAsString. When I run my test, I do see the JSON object but with all the \n and \r characters as well as a \ escaping all of the quotation marks. Obviously this means my JSON is no longer well formed.
Ideally I would like to find a reference for MunitTools so that I can see a list of functions that I can call and maybe find one that does not add the escape characters, but I haven't had any luck. If anybody knows of a some reference document that I can refer to, please let me know.
Not being able to find a way to return the data without the extra characters, I tried replacing them via dataweave. This is fine when replacing \n and \r, but as there are also more \s in front of each double quote and I can't seem to make these go away.
If I do this...
replace (/\/) with ("")
...I get an error. A co-worker suggested targeting the each \" and replacing them with ", but that's a problem because that gives me """. To get around this, I've tried
replace(/\"/) with "\""
...which does not cause any errors, but for some reason it reads the \ as a literal so it replaces the original string with itself. I've also tried...
replace(/\"/) with '"'
...but that also results in an error
I'm open to any other solutions as well.
Thanks
--Drew
I had the same concern so I started using the readUrl() method. This is a DataWeave method so you should be able to use it in any MUnit processor. Here is an example of how I used it in the set event processor. It reads the JSON file and then converts it into Java for my own needs but you can just replace java with JSON for your needs.
<munit:set-event doc:name="Set Event" doc:id="e7b1da19-f746-4964-a7ae-c23aedce5e6f" >
<munit:payload mediaType="application/java" value="#[output application/java --- readUrl('classpath://singleItemRequest.json','application/json')]"/>
</munit:set-event>
Here is the documentation for readUrl https://docs.mulesoft.com/mule-runtime/4.2/dw-core-functions-readurl
Hope that helps!
Follow this snippet (more specifically the munit-tools:then-return tag):
<munit-tools:mock-when doc:name="Mock GET /users" doc:id="89c8b7fb-1e94-446f-b9a0-ef7840333328" processor="http:request" >
<munit-tools:with-attributes >
<munit-tools:with-attribute attributeName="doc:name" whereValue="GET /users" />
</munit-tools:with-attributes>
<munit-tools:then-return>
<munit-tools:payload value="#[read(MunitTools::getResourceAsString('examples/responses/anypoint-get-users-response.json'), "application/json")]" />
</munit-tools:then-return>
</munit-tools:mock-when>
It mocks an HTTP request and returns a JSON object using the read() function.
I have a JSON data in the properties file and trying to retrieve it in java. When I am trying to retrieve the JSON data with the property name it's giving only first string/word from the JSON.
Inside the property file, I have the below content.
profile: {"fname": "ABC","lname": "XYZ","meetings":{"morning":10,"evening":60}}
I am trying to read the content using property name 'profile' as a string and I am getting below error message.
Expected ',' instead of ''
can someone help me with the issue, I tried to escape and unescape but still have the same issue
It may depend on what you are using to deserialize the JSON, but well formed JSON is a single element, so what you have needs to be inside of a container. That is, your file content should be:
{ profile: {"fname": "ABC","lname": "XYZ","meetings":{"morning":10,"evening":60}}}
You can do it like this:
profile={"fname": "ABC","lname": "XYZ","meetings":{"morning":10,"evening":60}}
Or if you want to do it in multiple lines
profile={\
"fname": "ABC",\
"lname": "XYZ",\
"meetings":{\
"morning":10,\
"evening":60\
}\
}
It works fine when I set myFileURL as "http://www.google.com/someimage.jpg", but when I try to use a variable I receive the error mesage.
My code is here: http://pastebin.com/QTHwu5tv
Solution
I was loading the data from an array result in a JSON query which added the brackets to the results.
Solution I was loading the data from an array result in a JSON query which added the brackets to the results.
after looking at your log. you need to provide a protocol for the file to download.
Try appending http:// at the beginning image path. it will be able to download then.