Mule-How to convert payload into json - json

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>

Related

Can't access json context property in WSO2 ESB after setting in java mediator?

I have a WSO2 xml file that calls a ResponseMediator file that contains the following mediator function:
public boolean mediate(MessageContext messageContext) {
final JsonObject json = new JsonObject();
final String name = "tom";
json.addProperty("id", 0);
json.addProperty("name", "tom");
json.addProperty("age", 32);
messageContext.setProperty("ID", json.get("id").getAsInt());
messageContext.setProperty("RESPONSE_BODY", json);
System.out.println("TESTING JSON : " + messageContext.getProperty("RESPONSE_BODY"));
return true;
}
I print after setting the json property to make sure the property has been set correctly, which it has. I also added the id separately as a property to make sure it wasn't only happening for json objects.
Then after returning back to WSO2 ESB, I try to log the values and the id comes through, but the json does not.
<log level="custom">
<property expression="$ctx:RESPONSE_BODY" name="LOGGING JSON PROPERTY"/>
</log>
<log level="custom">
<property expression="$ctx:ID" name="LOGGING ID PROPERTY"/>
</log>
[2020-12-09 10:35:23,434] [EI-Core] INFO - LogMediator LOGGING JSON PROPERTY =
[2020-12-09 10:35:23,434] [EI-Core] INFO - LogMediator LOGGING ID PROPERTY = 0
My question is, since the property is being set correctly in the mediator, what is the correct way to access it in WSO2? I've really only ever worked with passing single values so maybe I am missing something.
Looks like setting the property to a String using .toString() like so worked:
messageContext.setProperty("RESPONSE_BODY", json.toString());

Obtain value in json response

How do i write an expression to obtain the value "lastUpdated" in the below json response data
{"resourceType":"Parameters","parameter":[{"name":"medication","resource":{"resourceType":"Bundle","id":"956ffe6a-08ed-4cb6-82ca-41065a4a9923","meta":{"lastUpdated":"2020-08-24T19:09:18.5649325+00:00",
I have tried this but it does not work:
regex("\"lastUpdated\": \"(.*?)\"").saveAs("lastUpdated")
this also does not work:
jsonPath("$..[?(#.use==\"lastUpdated\")].value").saveAs("lastUpdated"))
Your input is a little cut off but here is what I've got:
myJSONString = '{"resourceType":"Parameters","parameter":[{"name":"medication","resource":{"resourceType":"Bundle","id":"956ffe6a-08ed-4cb6-82ca-41065a4a9923","meta":{"lastUpdated":"2020-08-24T19:09:18.5649325+00:00"}}}]}'
myJSONObject = JSON.parse(myJSONString)
myLastUpdated = myJSONObject.parameter[0].resource.meta.lastUpdated
console.log(myLastUpdated)
Basically you convert it from a json object into a javascript object. Then you can just traverse down the tree to your intended target.

How to convert payload into a json array in mule

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 />

use fastjson in SSH to return result

In ssh,when we want to return json data we can just set
<result name="success" type="json"></result>
in the struts xml.
But when my data has Timestamp values it return result like "2015-08-11T09:19:25Z";
Then I want to use fastjson to treat the data(has some other reasons),but I don't know to set fastjson into ssh so that I can return a json from fastjson rather than the json util itself.
looking forward to your answer
Above the get method of the timestamp value we can add
#JSON(format="yyyy-MM-dd HH:mm:ss")
public Timestamp getTest(){
return this.test;
}

Unmarshal JSON to Map/List of Strings

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.