I have this payload:
aaa,bbb,ccc,ddd,
I want to convert it into a json array so the output should look like this:
["aaa","bbb","ccc","ddd"]
When I try using the <json:object-to-json-transformer /> I get this:"aaa,bbb,ccc,ddd,", Any Ideas?
If your payload is a comma separated string, then first transform that to a List then using the object-to-json-transformer like so:
<set-payload value="#[Arrays.asList(payload.split(','))]" />
<json:object-to-json-transformer />
Related
I want to get a nested field in a json string using JSONPath.
Take for example the following json:
{
"ID": "2ac464eb-352f-4e36-8b9f-950a24bb9586",
"PAYLOAD": "{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
}
If I want to extract the #type field, I expect to do it like this
$.PAYLOAD.#type
But that doesn't seem to work..
Also tried this:
$.PAYLOAD['#type']
Do I need to use escape chars or something?
Posting my comment as an answer
"{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
Isn't JSON, it's a string containing encoded JSON.
Since JsonPath can't decode such string, you'll have to use a language of your desire to decode the string.
Eg: Decoding JSON String in Java
I have this:
{a=1, b=2, c=3}, {a2=1, b2=2, c2=3}, {a3=1, b3=2, c3=3},
I need an output of json like this:
[{"a":"1", "b":"2", "c":"3"}, {"a2":"1", "b2":"2", "c2":"3"}, {"a3":"1", "b3:2", "c3:3"}]
How to do so in Mule CE? Any Suggestions?
EDIT:
I have return data from magento <magento:list-products/>, and converted the payload like this:
<json:object-to-json-transformer doc:name="Object to JSON"/>
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
I had to do some filtering on that payload: used <foreach/> and for every product that fit the condition I saved in a variable like this:
<set-variable variableName="productsInfoArr"
value="#[flowVars.productsInfoArr.concat(flowVars.productInfo)+',']"/>
so the results are as above...
So basically the question is: how to create an array using json items?
I take it you payload is a Java Collection of Maps. If so, simply use the:
<json:object-to-json-transformer />
I usually use JSONArray to store the list of JSON object. In this case you might need to manipulate the input through Expression component:
<expression-component doc:name="Expression">
<![CDATA[
String input = payload.replaceAll("=", ":");
input = input.replaceAll(",$", "");
payload = new org.json.JSONArray("[" + input + "]");]]>
</expression-component>
I've 2 json feeds in payload (using Gather), i planned on using a groovy script to make it into a single json (I expected something like:
{key:value}{key:value})
<scripting:transformer doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[return '{"data":['+payload.toString().replace("}{","},{"+']}']]></scripting:script>
</scripting:transformer>
(expected output: {"data":[{key:value},{key:value}]}
But i get:
{"data":[[org.glassfish.grizzly.utils.BufferInputStream#102e37e, org.glassfish.grizzly.utils.BufferInputStream#a569d1]]}
W/O groovy script:
[org.glassfish.grizzly.utils.BufferInputStream#102e37e, org.glassfish.grizzly.utils.BufferInputStream#a569d1]
an array of inputstream
I tried using byte array to string, and object to string but it doesnt work, I dont figure out how can i solve this
Replace:
payload.toString().replace("}{","},{")
with:
payload.collect { it.text }.join(',')
Explanation: .text deserializes an input stream to a string, so payload.collect { it.text } will yield a collection of strings. Then join(',') takes care of concatenating these strings, separating them with ,
I am trying to do the following:
render [assignedSchol:assignedSchol,scholType:scholType] as JSON
assignedSchol is an object, and scholType is just a value. I get a "No map entry allowed at this place error". any help?
When you use one liner like you did you must put the conversion in braces like this:
render ( [assignedSchol:assignedSchol,scholType:scholType] as JSON )
But I think the above code returns json data as plain text format so I usually prefer doing it this way:
render(contentType: "text/json") {
[assignedSchol:assignedSchol,scholType:scholType]
}
Which gives me json data with response type json.
This should work:
render(contentType: "application/json") {[assignedSchol:assignedSchol,scholType:scholType]}
I would like to unmarshal a Json to a Map/List of Strings (eg Map>...)
Here is my input:
{"pointsOfSale":
{"pointOfSale":[
{"href":"\/pointsOfSale\/UUID.0abc2aca-7930-4c9e-9f38-8af3d0692e04",
"model":{"href":"\/models\/modelePointOfSale",
"modelType":{"href":"\/modelTypes\/pointOfSale"}},
"source":{"href":"\/sources\/TEST"},
"attribute":[
{"code":"pointOfSalePhysical","value":true},
{"code":"mail","value":"Mail1"},
{"code":"adresse","value":"address1"}]},
{"href":"\/pointsOfSale\/UUID.a12e7adf-652a-4197-91bf-d4785e43f09f",
"model":{"href":"\/models\/modelePointOfSale",
"modelType":{"href":"\/modelTypes\/pointOfSale"}},
"source":{"href":"\/sources\/Wikeo"},
"attribute":[
{"code":"pointOfSalePhysical","value":false},
{"code":"mail","value":"Mail1"},
{"code":"adresseMateriau","value":"Material address1"}]}
}}
I would like to be able to do "something" like this after unmarshaling:
myJsonMapped.get("pointsOfSale").get("pointOfSale").get(0).get("source").get("href").equals("\/sources\/TEST") == true
For instance, with Gson we can do this kind of decoding:
new Gson().fromJson(json, Map.class);
I know I can do this with a simple bean or processor etc...
I just want to know of I can do this more efficiently with a native JSON camel component config
EDIT: I tried different thing already like :
unmarshal().json()...
or
unmarshal().json(JsonLibrary.Gson, Map.class)..
etc...
without succes :'(
You could do something like this with jackson.
<dataFormats>
<json id="jack" library="Jackson"/>
</dataFormats>
...
<route>
<from uri="direct:test"/>
<unmarshal ref="jack"/>
<process ref="something"/>
</route>
Or in java with gson:
from("foo:bar")
.unmarshal().json(JsonLibrary.Gson,Map.class)
.to("foo:baz");
If you're not getting it to work, please state error and so fourth.