JSONPath Expression in wso2 esb - json

I have a json payload that looks like below:
{
"orders" : {
"order.id" : 1024,
"order.name" : "Ruffled-top"
}
}
I need to fetch order.id and order.name. Dot notation of JSONpath expression anyways won't work here. So I tried bracket notation like:
<property name="order-id" expression="json-eval($[orders][order.id])"/>
But the bracket notation is not working in wso2. Is there any other way this can be achieved? Thanks in advance.

You need to have order.id surrounded by '' inside the []. To access the orders object you can use the dot notation as well.
<property expression="json-eval($.orders.['order.id'])" name="order-id" scope="default" type="STRING"/>
or
<property expression="json-eval($['orders']['order.id'])" name="order-id" scope="default" type="STRING"/>

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());

Mule-How to convert payload into 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>

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

How to Index nested json objects in solr

I have a json object like this,
{
"id" : "123",
"name" : "aaa",
"loc" : {
"lat" : "",
"lon" : ""
},
}
In solr i added a field like
<field name="loc" type="string" indexed="true" stored="true" multiValued="true"/>
Then i tried adding the json doc to solr
But i couldn't see the 'loc' object being indexed in solr.
Am i doing wrong? Do i need to add anything in schema.xml
Can someone help me out of this pls?
I afraid you have to flatten the nested object to become something like
loc_lat = value of loc.lat
loc_lon = value of loc.lon
And of course, two new fields to store the data.
How are you adding your JSON? Because it does not look like the format required by Solr.
You need to conform to that. Also, if those lat/longs are really geographic coordinates, Solr has a dedicated geospatial support. Though you will need to get data into the appropriate shape either on the client side or with something like UpdateRequestProcessor, most likely ConcatFieldUpdateProcessorFactory.
post to
/solr/update/json?commit=true?split=/&f=txt:/**
see http://lucidworks.com/blog/schemaless-solr-part-1/

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.