how to parse a string with escaped json using dataweave 2.0 - json

I need to parse the response of an API that is like this:
"[{\"Customers\":[{\"Id\":1607715391563}],\"Partners\":[],\"ModDate\":\"\\/Date(1608031919597)\\/\",\"CreatedByUserId\":null},{\"Message\":null,\"Code\":\"200\",\"NextPage\":1}]"
I wish I have it like this:
[
{
"Customers":[
{
"Id":1607715391563
}
],
"Partners":[
],
"ModDate":"/Date(1608031919597)/",
"CreatedByUserId":null
},
{
"Message":null,
"Code":"200",
"NextPage":1
}
]
I already tried to remove the strings using payload[1 to -2], and parse the JSON using read(payload[1 to -2], 'application/json'). I already tried to follow some tips of this link but neither worked.
EDIT:
The point here is that I want to access, for example, Customers.Id value in other connector, and I can't

how about this?
%dw 2.0
output application/json
var inpString = "[{\"Customers\":[{\"Id\":1607715391563}],\"Partners\":[],\"ModDate\":\"\\/Date(1608031919597)\\/\",\"CreatedByUserId\":null},{\"Message\":null,\"Code\":\"200\",\"NextPage\":1}]"
---
read(inpString,"application/json")

You can try the following DataWeave expression:
%dw 2.0
output application/json
---
read((payload replace /^"|"$/ with '') replace '\"' with '"', "application/json")
The first replace will remove heading and trailing double quotes, and the second one will replace back slash scaped double quotes by double quotes.

Related

Parsing JSON with backslash node.js

I have this JSON file,
{
"file_paths": {
"PROCESS": "C:\incoming",
"FAILED": "C:\failed"
}
}
I get an error when I try to access PROCESS or FAILED. The error is SyntaxError: Unexpected token i in JSON. It must be due to backslash. How can I access PROCESS or FAILED without editing the JSON file?
You will need to escape the backslash in your JSON String.
If you are building the JSON yourself, you can escape special characters when you build it. OR if you are not, as a post process once you have the JSON file you could do something like sed over it to replace the backslash with an escaped backslash (obviously not the ideal solution).
The reason is because the JSON is not valid do to the \ no being escaped making the reader think the i is trying to be escaped
As J Livengood said, you need to escape backslashes when inside a string. Like so:
var obj = {
"file_paths": {
"PROCESS": "C:\\incoming",
"FAILED": "C:\\failed"
}
}

Mule (Dataweave) transform JSON array to string

This is probably simple, but everything I find in a search is only vaguely related to my question.
I have this:
{
"user":"C03999",
"caseNumber":"011-1234567",
"documents":[
{
"file":[
{"name":"indem1","document":"xSFSF%%GSF","mimeType":"PDF"},
{"name":"indem2","document":"xSFSF%%GSF","mimeType":"PDF"}
]
}
],
"mortgagee":"22995",
"billTo":"Originator",
"agreementDate":"2016-11-25",
"term":360,
"expirationDate":"2017-11-25",
"startDate":"Agreement",
"lenderEndorsed":true,
"lenderExecutedAgreement":false,
"indemnificationAgreementTransferable":false,
"qadLrsFileNumber":"2016-99999-9999",
"docketNumber":"2016-9999-99"
}
I would like to get this string out of it:
indem1,indem2
I created a global function:
<configuration doc:name="Configuration">
<expression-language autoResolveVariables="true">
<global-functions>
def convertToString(data){
return data.toString();
}
</global-functions>
</expression-language>
</configuration>
My transform looks like this:
%dw 1.0
%output application/csv
---
payload.documents.file map {
"" : convertToString($.name)
}
My output looks like:
[indem1\, indem2]
What do I need to do to get my desired string (indem1,indem2)?
Your question title says that " JSON array To string" but the dataweave code attached above has application/csv in the output.
Are you trying to convert into csv?? If that is the expectation, comma is a reserved character for .csv format and that is the reason , is getting escaped with backslash[indem1\,indem2].
If your intention is to convert into java, here is the code that returns String value.
%dw 1.0
%output application/java
payload.documents[0].file.*name joinBy ','
In general, if you have an object, and you need PART of that object to be written in a given format / MIME-type, use the write() function in the dw::Core module. If you need to read some content in an object that is JSON/JAVA/CSV and the rest is some other format, use the read() function in the same module.
For example, suppose you have payload as:
{
field1: "foo",
field2: { nested: "value" }
}
Further, suppose that you want the object to be JAVA, but the "field2" value to be seen as JSON. You can use this DW:
output application/java
---
{
field1: payload.field1,
field2: write(payload.field2,'application/json')
}
The result is:
{
field1: "foo",
field2: '{ "nested": "value" }'
}
NOTICE that "field2" now shows up as a nested JSON object, not a JAVA object. One way to tell is that the key in the JSON object is in quotes, but in a JAVA object they are not.

Nested CSV - Mule DataWeave

I have a CSV like this:
data1,data2,data3;dataa;datab;datac;datax,datay,dataz
data1,data2,data3;dataa;datab;datac;datax,datay,dataz
data1,data2,data3;dataa;datab;datac;datax,datay,dataz
I use spliter to process the records line by line, further I use splitBy "," in dataweave to convert the record to a map. But how I can do another level of split for ";" ? SplitBy doesnt allow muliple delimiters so do the CSV type in dataweave.
Ultimately, I want a JSON like this:
{
"1":"data1",
"2":"data2",
"3":{
"a":"dataa",
"b":"datab",
"c":"datac"
},
"x":"datax",
"y":"datay",
"z":"dataz "
}
Any thoughts ?
Try the following DataWeave code:
%dw 1.0
%output application/json
---
payload map {
"1": $[0],
"2": $[1],
"3": using (detail = $[2] splitBy ";") {
a: detail[1],
b: detail[2],
c: detail[3]
},
x: $[3],
y: $[4],
z: $[5]
}
Notes:
I modified the input data to separate datac and datax. Replace the ; character with , e.g.: ...;datab;datac,datax,...
I use File connector to read the CSV file, and directly process it in DataWeave transformer (do not use a Splitter)
I want to observe, that your JSON example has bad structure!
In this JSON the 4th element is an object and it hasn't a key, just value...
First of all, u should validate your end JSON.
Example of your valid JSON:
When u validate your JSON, I'll try to help in convering your CSV data to the JSON.

Are line feeds allowed in JSON strings?

I need to send XML inside a JSON for my REST OSB 12c Proxy as follow:
{
"login": "jstein",
"identityContext": "jazn.com",
"taskId": "string",
"payload": {
"any_0": {
"any_01": "<afastamento xmlns:ns1='http: //www.tjsc.jus.br/soa/schemas/comagis/AfastamentoMagistrado' xsi:type='def: AfastamentoMagistradoType' xmlns:xsi='http: //www.w3.org/2001/XMLSchema-instance' xmlns='http: //xmlns.oracle.com/bpel/workflow/task'>
<ns1:Magistrado>719</ns1:Magistrado>
<ns1:Status>Inicial</ns1:Status>
<ns1:Vaga>8770</ns1:Vaga>
<ns1:Tipo>Licenca Nojo</ns1:Tipo>
<ns1:PeriodoReferencia/>
<ns1:DataInicialSolicitada>2015-10-10</ns1:DataInicialSolicitada>
<ns1:DataFinalSolicitada>2015-11-05</ns1:DataFinalSolicitada>
</afastamento>"
}
},
"outcome": "Start"
}
The OSB 12c send me back the error:
"errorMessage" : "ORABPEL-15235\n\nTranslation Failure.\nFailed to translate
JSON to XML. org.codehaus.jackson.JsonParseException: Illegal unquoted
character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be
included in string value\n at [Source: java.io.BufferedReader#7db921c7; line:
7, column: 619]\nThe incoming data does not conform to the NXSD schema. Please correct the problem.\n"
I am testing my JSON request at JSONLint, and it always gives me the error about start a String with <:
Parse error on line 7:
"any_01": "<afastamento xmlns:
-----------^
Expecting 'STRING, 'NUMBER, 'NULL', 'TRUE', FALSE', '{', '['
No, literal line feeds (CTRL-CHAR, code 10) and newlines are control characters that are not allowed within a JSON string:
XML does not require the line feeds between elements. You can simply remove them, changing your multi-line XML document to an equivalent single-line XML document that will be able to be passed as a JSON string without problem. Or, you may want to consider escaping the line feeds \n, or more generally, escaping the entire string:
How should I escape strings in JSON? [Java]
In C# how to encode XML to output it inside JSON in the JavaScript
part of a page

Why does the Groovy JSONBuilder escape slashes in URLs?

I am writing a Groovy script that needs to POST JSON to a URL. I have noticed a problem were all elements in my JSON that contains a '/' are changed to '\/' by the JSON Builder. Is there a way to stop this?
This is using Groovy 1.8. Here is a simple example and its output:
def json = new JsonBuilder()
json.reply {
result 'http://google.ie/testing'
}
println json.toString()
Output -> {"reply":{"result":"http:\/\/google.ie\/testing"}}
Thanks
Just had a look, and groovy.json.JsonOuput.toJson(string) encodes forward slash as '\\/'.
You can use toPrettyString and it doesn't do it however:
def json = new groovy.json.JsonBuilder()
json.reply {
result 'http://google.ie/testing'
}
assert json.toPrettyString() == '''{
"reply": {
"result": "http://google.ie/testing"
}
}'''
Why does the Groovy JSONBuilder escape slashes in URLs?
An excerpt of the interesting points made in http://groups.google.com/group/opensocial-and-gadgets-spec/browse_thread/thread/1642ec0bed9b95ba/21956eed23f04e13?pli=1 on this topic:
Arne Roomann-Kurrik: Per the JSON spec, escaping '/' is optional.
Mike Samuel: The solidus is among the set of characters that MAY be escaped so that it is safe to embed the JSON substring </script> in HTML as <\/script>. (Half of this quote is by Andrea Ercolino.)
Kevin Brown: This is primarily due to buggy javascript parsers that treat // as a comment
when it's in a string.