how to access json data mule esb - json

i want to access json data generated from the sync flow into an async flow.
I am getting json data from sync flow correctly and i want to fetch certain attribute value from that my json data is as follows :
{"data" : [{"in_timestamp":"2012-12-04","message":"hello","out_timestamp":null,"from_user":"user2","ID":43,"to_user":"user1"}]} and to user is #[json:to_user]}
I want to access to_user attribute from this json format.
I have tried using #[json:to_user] but it simply prints it as a string and doesnt return any value.
Please help. Thanks in advance.

The right expression based on your sample JSON is:
#[json:data[0]/to_user]

JsonPath expression are depreciated for now and you will even not get enough document on it for doing ..
So, currently you need to use either :- <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
or <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
or even <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object" /> to extract data from JSON depending on the JSON data

Related

Camel - json body is consumed after have used jsonpath

i'm using camel in a rest context and i've to manipulate a json got from a request . It's something like:
{
'field1':'abc',
'field2':'def'
}
All i've to do is to extract field1 and field2 and put them in 2 properties, so i tried something like that
<setProperty propertyName="Field1">
<jsonpath>$.field1</jsonpath>
</setProperty>
<setProperty propertyName="Field2">
<jsonpath>$.field2</jsonpath>
</setProperty>
but i get this error:
org.apache.camel.ExpressionEvaluationException:
com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['field2'] in path $ but found 'java.lang.String'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
and after some tests i found out my body was empty after the first use of jsonpath.
The same process applied to an XML using xpath doesn't give any error, and i'm wondering if it's possible to do the same with jsonpath instead to create a mapper object in java. thank you in advance
If the processed Camel message is of type InputStream, this stream can obviously be read only once.
To solve this:
either enable Camel stream caching (http://camel.apache.org/stream-caching.html)
or insert a step (before jsonpath queries) in your route to convert message body to a string (so that it can be read multiple times:
(eg <convertBodyTo type="java.lang.String" charset="ISO-8859-1">) )

How to exclude null values while converting object to json

I am working with MuleSoft Anypoint Studio and I need to convert JSON payload to in the end XML. During this conversion every field that is NULL need to be excluded. Some values are not sent via POST request and I am expecting to not see them in the end result - XML file but that is not the case as they are there. For example in the JSON POST request Value field is not sent, which becomes null in Mule so it should not appear in the XML file but it's still written in it as <Value/>. I am mainly having problems with Object to JSON transformer.
I have tried configuring a custom mapper
<spring:beans>
<spring:bean id="Bean" name="NonNullMapper" class="org.codehaus.jackson.map.ObjectMapper">
<spring:property name="SerializationInclusion">
<spring:value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</spring:value>
</spring:property>
</spring:bean>
But that didn't really work. I also tried
<spring:beans>
<spring:bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<spring:bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<spring:property name="targetObject" ref="jacksonObjectMapper" />
<spring:property name="targetMethod" value="configure" />
<spring:property name="arguments">
<spring:list>
<spring:value>WRITE_NULL_MAP_VALUES</spring:value>
<spring:value>false</spring:value>
</spring:list>
</spring:property>
</spring:bean>
</spring:beans>
That didn't work too as I get an error which I couldn't manage to fix
More than one object of type class org.codehaus.jackson.map.ObjectMapper registered but only one expected
I am working with
Mule 3.9.0
Anypoint Studio 6.4
com.fasterxml.jackson and in some places org.codehaus.jackson
I would really appreciate any help or some hint.
Given that this in Mule, you can use DataWeave instead to transform the payload. Setting the XML writer property skipNullOn could give the desired result. https://docs.mulesoft.com/mule-user-guide/v/3.9/dataweave-formats#skip-null-on
Example
%output application/xml skipNullOn="payload"
---
payload

Orbeon autocomplete display JSON response

I'm trying to fill the autocomplete field in Orbeon (version 2016.1) with suggestions which I receive as a JSON.
The JSON I get looks like:
{"status":"success","code":200,"data":{"streets":[{"name":"Street One","id":"1"},{"name":"Street Two","id":"2"},{"name":"Street Three","id":"3"}]}}
I know that the Resource URI should point to my web service (could that URI, or the arguments I need to send, be encoded?), but I don't know how the Items, Label and Value fields should be configured in this case (the label would be name from the json and value should point to the code from the json, of course).
I referred to https://doc.orbeon.com/xforms/submission-json.html but haven't exactly managed to get what I'm trying to.
Can someone help?
Thanks in advance.
Masa
In particular, with your specific JSON, the corresponding XML will look as follows. In general, see the section Seeing the converted XML for how you can create a form in Form Builder that allows you to see what the converted XML is for any JSON.
<json type="object">
<status>success</status>
<code type="number">200</code>
<data type="object">
<streets type="array">
<_ type="object">
<name>Street One</name>
<id>1</id>
</_>
<_ type="object">
<name>Street Two</name>
<id>2</id>
</_>
<_ type="object">
<name>Street Three</name>
<id>3</id>
</_>
</streets>
</data>
</json>

Netsuite connector in mule "returnSearchColumns" attribute

I created a saved search of "items" in netsuite.
<netsuite:search config-ref="NetSuite__Login_Authentication" searchRecord="ITEM_ADVANCED" bodyFieldsOnly="false" returnSearchColumns="true" doc:name="NetSuite"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
When 'returnSearchColumns' is set to "true", receiving the below exception. If this attribute is set to false, there is no exception but response is missing the columns selected.
java.lang.IllegalArgumentException: No enum constant org.mule.module.netsuite.RecordTypeEnum.ITEM
Also, received 'ConsumerIterator' object as response from netsuite and used "Object to JSON" transformer right after netsuite connector. The response received is an array of item objects.
1) Is there a way to convert this payload into XML format? Both object to XML and JSON to XML are not giving entire XML.
2) How to avoid the above mentioned illegal argument exception ?
1) object-to-xml should convert all fields to XML, or you could try something like Dataweave. What exactly is missing?
2) There is no type called 'ITEM'. You have to use one mentioned in this list: http://mulesoft.github.io/netsuite-connector/6.0.1/java/org/mule/module/netsuite/RecordTypeEnum.html such as 'INVENTORY_ITEM '

Mule Studio, Transform byte array into MySQL

I've got a Magento connection up and running and want to get all customers.
My subflow looks like this:
<sub-flow name="listCustomers" doc:name="listCustomers">
<magento:list-customers config-ref="MagentoConnecter" doc:name="Magento"/>
<byte-array-to-object-transformer doc:name="Byte Array to Object"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</sub-flow>
which results into a string. But I'd like to insert the variables/customer data into a MySQL.
Do I need to use a foreach component?
And how can I address the variables then?
Thanks,
Chris
Foreach seems like a good way to achieve that.
The step to do are the following:
Transform the JSON representation into a list of maps using the JSON Transformer (the returnClass will be java.util.Map)
Introduce the foreach scope
Within this scope insert a jdbc outbound endpoint that performs the insert query