How to exclude null values while converting object to json - 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

Related

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>

Prefixing JSON in MappingJackson2HttpMessageConverter

I've using Spring/AngularJS and to prevent JSON vulnerability, I'm trying to prefix all JSON array responses with ")]}',\n" - see reference.
I was able to prefix by
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
<property name="jsonPrefix" value=")]}',\n" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
But the problem is it's prefixing all JSON responses with ")]}',\n" and I only need to prefix the JSON arrays. Is there a way I could only set the prefix for JSON array responses? Thanks.
Instead of having a prefix which basically makes your response invalid JSON consider returning a object instead of an array. This will mitigate the attack vector as well.
{d: [1,2,3,4]}

JSONPath expression for checking string in Apache Camel XML

Let's say I have a simple json file such as the following
{
"log": {
"host": "blah",
"severity": "INFO",
"system": "1"
}
}
I'm using Apache Camel, and it's Spring XML to process and route the json file. My routing code looks something like this:
<route>
<from uri="file:/TESTFOLDER/input"/>
<choice>
<when>
<jsonpath>$.log?(#.severity == 'WARNING')</jsonpath>
<to uri="smtp://(smtpinfo...not important)"/>
</when>
<otherwise>
<to uri="file:/TESTFOLDER/output"/>
</otherwise>
</choice>
</route>
The part that I'm really confused about is the JSONPath expression. The expression I have above isn't even syntactically correct, because its hard to find examples for the case where you aren't trying to sort through a list of elements. My goal is to only send an email if the severity of the log is 'WARNING' but I can't come up with the expression.
This worked for me using Camel 2.13.1 (I checked for INFO as your JSON example has this severity; you may change this according to your needs):
<jsonpath>$..log[?(#.severity == 'INFO')]</jsonpath>
Note the .. and the []. However, using a single dot . at the beginning of the search path failed:
<jsonpath>$.log[?(#.severity == 'INFO')]</jsonpath>
The error messages said:
java.lang.IllegalArgumentException: Invalid container object
This may be a bug.
According to the JSON Path doc, .. stands for "recursive descent". This may not meet your requirements. However, as a single dot . didn't work, this was the only possible work around I figured out. Otherwise, you may rise a bug ticket.

how to access json data mule esb

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

how to save serializable object to database field

I'm using linq to sql and I have the need to save a few of our serializable objects to a field in our sql server database. I've defined the field as varbinary(MAX) but I'm not 100% sure that's correct. So, does anyone know how I can save an object to this field and then read it back? Thanks.
I think this depends on the format the objects are serialized in. For instance, if you are serializing out to xml then sql server (at least the latest version) supports an xml type. If you are indeed serializing out to binary, then sticking with binary is probably okay. Picking a db type that is similar to the type of the serialization will minimize a transformation bridging the db type to the object type.
I have gotten the object to serialize but I'm running into a problem getting the resulting xml saved. The object field is defined as xml so in linq to sql it's an xelement. I'm using the following code to serialize and get the xml string. But when I try to load the xml string to the xelement I get the "Illegal characters in path" error. However when I look at the generated xml I don't see any illegal characters. Here is the generated xml.
using (StringWriter sw = new StringWriter())
{
var x = new XmlSerializer(sessionObject.ObjectToStore.GetType());
x.Serialize(sw, sessionObject.ObjectToStore);
sessionRecord.sessionObject = XElement.Load(sw.ToString());
}
<?xml version="1.0" encoding="utf-16"?>
<SerializableJobSearch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Keywords>electronics</Keywords>
<StateList />
<Radius>50</Radius>
<City />
<State />
<DisciplineIdList />
<IndustryIdList />
<Direct>false</Direct>
<TempHire>false</TempHire>
<Contract>false</Contract>
<PerHour>false</PerHour>
<PerYear>false</PerYear>
<DegreeLevel />
<IncludeJobsRequiringLess>false</IncludeJobsRequiringLess>
<AddedWithin>0</AddedWithin>
<OrderBy>Rank</OrderBy>
<OrderByRank>true</OrderByRank>
<resultsPerPage>50</resultsPerPage>
<NeedToMake />
</SerializableJobSearch>