I'm trying convert to csv to json format using mule datamapper ,it is working fine.
below output it produce
[ {
"propertyState" : "AL",
"propertyCity" : "NJ",
"propertyZipCode" : "67890",
"propertyCounty" : "US"
} ]
But want to remove [ ] this from json format. using datamapper is it possible
mule modify json output datamapper
[] defines List for the elements .. That is required if your elements are repeating and a Valid JSON format ..
If you don't want the [] to be there then the work around will be use <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to List" /> to extract each elements value from the JSON payload and create your payload using expression ..
But again this is not a recommended approach since you always require [] in your JSON as you will be getting multiple rows from CSV file and the JSON format should have [] since that will represent it as List which is a valid format
Related
I am reading a parquet file and trying to extract elements within a Struct of Struct of Array. The null values however are returning empty when I use getItem(). This pattern works in Spark 1.6 but now using Spark 2.4 in aws glue it seems to ignore the null values and only pulls empty.
Input is parquet but I have written in JSON format:
{
"ExampleMessage":{
"activity":{
"exampleSport":[
{
"exampleRole":null
},
{
"exampleRole":null
},
{
"exampleRole":"Runner"
}
]
}
}
Attempted Extraction:
raw_df.select(col("ExampleMessage.activity").getItem("exampleSport").getItem("exampleRole"))
Current Output:
,,Runner
Desired Output:
null,null,Runner
Try the below code instead of null you will get None
raw_df.select(col("ExampleMessage.activity").getItem("exampleSport").getItem("exampleRole")).head()
output will be
Row(ExampleMessage.activity AS `activity`.exampleSport.exampleRole=[None, None, 'Runner'])
I wanted to output json data not as array object and I did the changes mentioned in the pentaho document, but the output is always array even for the single set of values. I am using PDI 9.1 and I tested using the ktr from the below link
https://wiki.pentaho.com/download/attachments/25043814/json_output.ktr?version=1&modificationDate=1389259055000&api=v2
below statement is from https://wiki.pentaho.com/display/EAI/JSON+output
Another special case is when 'Nr. rows in a block' = 1.
If used with empty json block name output will looks like:
{
"name" : "item",
"value" : 25
}
My output comes like below
{ "": [ {"name":"item","value":25} ] }
I have resolved myself. I have added another JSON input step and defined as below
$.wellDesign[0] to get the array as string object
I am trying to get rows from database and send the response in "CSV" format.
This is the transform component right after database select
%dw 2.0
output application/csv
---
payload map {
name : $.NAME,
id: $.Num,
age: $.age
}
When I hit the API using postman/soapUI, I get response as json, not csv.
Something like this maybe? List> converted to CSV (assuming you were issuing a select from the DB)
I'm using Mule Requester to load a CSV file. After the file is loaded, the payload is a byte array, which I store in a flow variable, mycsv. I keep getting an exception.
org.mule.api.MessagingException: Exception while executing:
NetIds: flowVars.mycsv map $."My Column Name"
^
Cannot coerce a :binary to a :array (com.mulesoft.weave.mule.exception.WeaveExecutionException). Message payload is of type: byte[]
Here's my DataWeave code:
%dw 1.0
%output application/java
---
{
Values: flowVars.mycsv map $."My Column Name"
}
The previous flow element is a choice, so I set the metadata on that to output to a FlowVar with the right name and referenced a sample CSV file, so DataWeave things the variable type is List<Csv>.
How can I read the CSV? Thanks!
It's because it doesnt know the mimeType of the flowVar because it's not set. Try this before the dataweave transformer:
<set-variable value="#[flowVars.mycsv]" variableName="mycsv" mimeType="application/csv" doc:name="Variable" />
or set the mimeType when you first read the csv.
This is the first time I found a requirement to put two different formats into one csv file. In order to get the expected result, I prefer to use this trick:
Create both expressions and combine them into one simple array, using flatten
Add an empty object as line separator
I have a JDBC COnnector which retrieves values from the database and I have mapped them from object to JSON. Now I want to extract specific values from the json to flow variables. When I try to log #[message.payload], I get the full payload in the log which is in JSON format. However when I try to choose an attribute (eg. testattribute in json) #[message.payload.testattribute], I get mule expression error. How do I refer to the json values?
Once the payload is a JSON string, you can't extract anything from it anymore with an expression.
So either:
use MEL to extract the values into flow variables prior to convert the payload into JSON,
transform the JSON payload to either POJOs or Map/Lists, use MEL to extract the values into flow variables, and re-transform the payload back to JSON.
If your requirement is to get the values from json data you can use a simple MEL expression like
#[json:<name of the node>]
For example if you have an sample json data like this
{
"token" : 123,
"id" : 456,
"email" : "abc#abc.com",
"status" : "Success"
}
and if you want get the id from the data just use the below simple MEL.
#[json:'id']
Mule has JSON-to-Object transformer which can be used to get JSON elements.
For example if your JSON is following :-
{
"token" : 123,
"id" : 456,
"email" : "abc#abc.com",
"status" : "Success"
}
Now, to extract the elements, you need to use :-
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
And then can extract like :- #[message.payload.email] or #[message.payload.status]
Use components as follow
Object - String
Object - JSON
then u can directly extract using value using json expression as #{json:email},
in below xml i have used the variable component to set the #{json:email} in "var1" flow variable for usability
Note: json expression is even available in studio version of 5.x
<object-to-string-transformer doc:name="Object to String"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<set-variable variableName="var1" value="#[json:email]" doc:name="Variable"/>