Adding parent xml node to json using xslt version 1.0 - json

I need to add parent xml to json content using xslt version 1.0. I have tried multiple ways to get this but no luck.
**Input Content:**
------------------
{
"EmployerRegistrationReq": {
"EmployerRegistrationReq": {
"EmployerRegistrationHeader": {
"CorporateID": "SIT0001",
"CorpReferenceNumber": 48499964
}
}
}
}
**Expected Output:**
--------------------
<Library>
{
"EmployerRegistrationReq": {
"EmployerRegistrationReq": {
"EmployerRegistrationHeader": {
"CorporateID": "SIT0001",
"CorpReferenceNumber": 48499964
}
}
}
}
</Library>
Can anyone help me here:

In XSLT 1.0, this will be simple textual manipulation. You won't be able to treat your JSON as a structured item, at least, not without some additional libraries.
If your only problem is to take a "string" value, here a string that can be a serialization of a JSON value, and to make it the text() node child of an element <Library> then the problem is simply one of passing your JSON String text as a parameter to an XSLT 1.0 transformation and then use this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="jsonString" />
<xsl:template match="/" >
<Library><xsl:value-of select="$jsonString" /></Library>
</xsl:template>
</xsl:stylesheet>

You can use an external entity, I think:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ENTITY json SYSTEM "json-sample1.json">
]>
<root>&json;</root>
Caveat: use of DTDs/entities might be restricted/disabled these days due to security default settings.

Related

Map file in the Integration for the LogicApp in Azure

Have the following XML as a result of the HTTP GET function from the B2B supplier.
<Invoices xmlns="http://gateway.com/schemas/Invoices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://gateway..com/schemas/Invoices Invoices.xsd">
<DocumentInfo>
<DocType>INVOICES</DocType>
<DocVersion>2.0</DocVersion>
</DocumentInfo>
<Header>
<StartDate>2018-12-01T00:00:00+01:00</StartDate>
<EndDate>2019-01-03T00:00:00+01:00</EndDate>
</Header>
<Documents>
<Invoice InvoiceId="RP82807" InvoiceDate="2019-01-02T00:00:00+01:00" DocumentType="IN" RefDocId="FT34532" RefDocType="ORDER" SystemId="10" HasPDFImage="0" />
<Invoice InvoiceId="T609881" InvoiceDate="2018-12-31T00:00:00+01:00" DocumentType="IN" RefDocId="FT39339" RefDocType="ORDER" SystemId="0" HasPDFImage="0" />
</Documents>
</Invoices>
Based on this article I've created the liquid map file to get the list of InvoiceIds:
{
"Invoice": "{{content.Documents.Invoice}}"
}
When using it in the LogicApp in the XML->Json converter, got the following result:
{
"Invoice": ""
}
I have also tried this as a liquid file:
{
"Invoice": "{{content.Invoices.Documents}}"
}
and this:
{
"Invoice": "{{content.Invoices.Documents.Invoice}}"
}
with the same result.
Can you give me a tip what I do wrong?
I tried to transfer part of your xml file to json with this map:
{
"DocType":"{{content.DocumentInfo.DocType}}",
"Invoice":"{{content.Documents.Invoice}}"
}
And get the output:
{
"DocType": "INVOICES",
"Invoice": ""
}
So this means I can get the DocType but can't get Invoice properties, so I think maybe the Liquid map doesn't support the XML format. Maybe you could change it to like this:
<Invoice>
<InvoiceId>T609881</InvoiceId>
<InvoiceDate>2018-12-31T00:00:00+01:00</InvoiceDate>
<DocumentType>IN</DocumentType>
<RefDocId>FT39339</RefDocId>
</Invoice>
This will work, or you could go to Liquid reference to check is there any methods to match the properties.
Note:what you want for now,binding to Xml Attributes is not currently supported.You could refer to this answer.
If you still have other questions, please let me know.
UPDATE:You still could do it with logic app. For example I used a FTP connector to get xml file content then create a json with "json(xml(body('Get_file_content')))" expression.
And this is the result.

How to rename a key in JSON

Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<p:Order xmlns:p="http://no.lyse.ikt.altisalg.Order" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="filename.xsd" source="AltiSalg 2.4.4-IB-154-IB-154 (154)"><p:Customer><p:OrderId></p:OrderId>
<p:FirstName></p:FirstName>
<p:LastName></p:LastName>
<p:EmailAddress></p:EmailAddress>
<p:BirthDate></p:BirthDate>
<p:CellularPhone></p:CellularPhone>
<p:City></p:City>
<p:Country></p:Country>
<p:PostalCode></p:PostalCode>
<p:StreetAddress></p:StreetAddress>
<p:PartnerId></p:PartnerId>
<p:CommunicationPreferences><p:Phone></p:Phone>
<p:Email></p:Email>
<p:Mail></p:Mail>
<p:SMS></p:SMS>
</p:CommunicationPreferences>
<p:ServiceAgreement><p:AgreementType></p:AgreementType>
<p:AgreementStartDate></p:AgreementStartDate>
<p:AgreementStatus></p:AgreementStatus>
<p:Comment></p:Comment>
<p:ListOfAssets><p:Asset><p:ProductPartNumber></p:ProductPartNumber>
<p:Comments></p:Comments>
</p:Asset>
<p:Asset><p:ProductPartNumber></p:ProductPartNumber>
<p:Comments></p:Comments>
</p:Asset>
<p:Asset><p:ProductPartNumber></p:ProductPartNumber>
<p:Comments></p:Comments>
</p:Asset>
<p:Asset><p:ProductPartNumber></p:ProductPartNumber>
<p:Comments></p:Comments>
</p:Asset>
<p:Asset><p:ProductPartNumber></p:ProductPartNumber>
<p:Comments></p:Comments>
</p:Asset>
<p:Asset><p:ProductPartNumber></p:ProductPartNumber>
<p:Comments></p:Comments>
</p:Asset>
</p:ListOfAssets>
</p:ServiceAgreement>
</p:Customer>
</p:Order>
When i am trying to convert into xmltojson i got below screen shot keys.
i need to remove p: from all keys. i am unable to access key because of p: is coming. When i am trying to check online converting its working and its not remove from my code. i am working on node and using "xml-to-json-promise" Module
https://prnt.sc/hvzrzf
Note : I can not any change in XML file.
xml-to-json-promise is based on the xml2js package (which itself is based on sax-js). I use xml2js and promisify it myself, but the trick is the options.tagNameProcessors. Read about them in the docs
Use it like this:
const prefixMatch = new RegExp(/(?!xmlns)^.*:/)
function stripPrefix(str) {
return str.replace(prefixMatch, '');
}
convert.xmlDataToJSON(xml, {
tagNameProcessors: [stripPrefix]
// other options too
})
This is included in xml2js package and could be required like so:
const {stripPrefix} = require('xml2js/lib/processors')
Anyway, there ya go! Tag names are processed to rename the key (removing any prior prefix).

XPath/JSONPath search of arrays

Our REST API is returning an array of nested objects. Using XPath or JSONPath, I would like to extract the id elements of each top level array object but not the children's id elements.
[
{
"id":"1",
"child":{
"id":"a"
}
},
{
"id":"2",
"child":{
"id":"b"
}
}
]
The expected output is 1, 2 and NOT 1, a, 2, b.
Can anybody help with the query syntax? I have the example at http://jsfiddle.net/dqvrfvc1/2/
Try selecting the id at a specific level:
search = JSON.search(data, '/*/*/id')
See the update here: http://jsfiddle.net/dqvrfvc1/5/
If we dump the XML to the console (console.log(JSON.toXML(data));) we see:
<d:data xmlns:d="defiant-namespace" d:constr="Array" d:mi="9">
<d:item d:mi="4">
<id d:constr="String" d:mi="1">1</id>
<child d:mi="3">
<id d:constr="String" d:mi="2">a</id>
</child>
</d:item>
<d:item d:mi="8">
<id d:constr="String" d:mi="5">2</id>
<child d:mi="7">
<id d:constr="String" d:mi="6">b</id>
</child>
</d:item>
</d:data>
This means that instead of /*/*/id, we can be even more specific with:
search = JSON.search(data, '/d:data/d:item/id')
Note: Namespace selection isn't possible, so there doesn't seem to be the need to bind the d: namespace prefix to the defiant-namespace uri.
Also, take a look at the "XPATH EVALUATOR" section of http://defiantjs.com and switch between XML and JSON views to see how the JSON is represented in XML. This will help you understand the the data structure and at what level id would be found.
Given you have jmeter in your tags here is a solution for it:
JSON Path Expression should look like: $[*].id
Demo:
References:
JSON Extractor
JSON Path: Getting Started
API Testing With JMeter and the JSON Extractor
You mentioned XPath: in XPath 3.1 this is
parse-json($data)?*?id

WCF service response from "html" to "xml"

trying to translate from one asmx web service to svc web service. same code, but got the following difference in responce
when using asmx:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.MyKidsSpending.com/">-2202.68</string>
when using svc:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">-2202.68</string>
where did the first line go?
Here is the fist couple of lines of the function that I am testing in the svc service....
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
public string Balance(string Uid)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
I was suggested that the response was html, that is why it doesn't have
<?xml version="1.0" encoding="utf-8"?>
now I change text/html to text/xml, why doesn't still match? Suggestions? Thanks

DataWeave transformation issue in esb mule

I have transformation in dataweave.
I am getting input from the Mysql database. Input payload type is List- Map. My dataweave editer has below code.
%dw 1.0
%namespace ns1 urn:abc:api:Components
%output application/xml
---
ns1#"ItemRequest":{
ns1#"Requester":
ns1#ac: payload.time_max,
ns1#"ErrorLanguage": "en_US",
ns1#"WarningLevel": "High"
}
My output is
<?xml version='1.0' encoding='UTF-8'?>
<ns1:ItemRequest xmlns:ns1="urn:abc:api:Components">
<ns1:Requester>
<ns1:ac>
<time_max>1</time_max>
</ns1:ac>
</ns1:Requester>
<ns1:ErrorLanguage>en_US</ns1:ErrorLanguage>
<ns1:WarningLevel>High</ns1:WarningLevel>
</ns1:AddItemRequest>
I am not sure why i am getting tag created under'ac' element in my output with 'time_max'. The 'Time_max' is the column name in MySQL table. But i dont want to add the column in the output xml. How can we avoid this tag creation ?
Expected output is
<?xml version='1.0' encoding='UTF-8'?>
<ns1:ItemRequest xmlns:ns1="urn:abc:api:Components">
<ns1:Requester>
<ns1:ac>1</ns1:ac>
</ns1:Requester>
<ns1:ErrorLanguage>en_US</ns1:ErrorLanguage>
<ns1:WarningLevel>High</ns1:WarningLevel>
</ns1:AddItemRequest>
I have resolved the issue by typing below code.
%dw 1.0
%namespace ns1 urn:abc:api:Components
%output application/xml
---
ns1#"ItemRequest":{
ns1#"Requester":
ns1#ac: payload[0].time_max,
ns1#"ErrorLanguage": "en_US",
ns1#"WarningLevel": "High"
}
The below line of snippet is did magic. I have included array index values.
ns1#ac: payload[0].time_max