Include a json file in an another json file only with json - json

In xml i can include a file in a another xml file and use it. This is usefull if your software get the configuration file from xml but doesn't have any method to separate configuration like apache/ngnix(nginx.conf - site-available/ - site-enable/).
file A:
<!DOCTYPE icecast [
<!ENTITY mount SYSTEM "mount.xml">
]>
<icecast>
...
&mount;
...
</icecast>
file B:
<mount>
<mount-name>/xyz</mount-name>
<username>source</username>
<password>password</password>
<max-listeners>30</max-listeners>
<hidden>0</hidden>
<public>1</public>
<no-yp>0</no-yp>
</mount>
But what if the configuration file is in json format? Is there a method equivalent?
I need to have a better organization of my couchbase-sync bucket configuration.

There is no mechanism in JSON for including another file, like you describe. JSON is not a language in the sense that XML is, it's just the JavaScript object notation, so it only serves to describe application objects, nothing else.

Related

How to JSON wrapper ( {"text": " ) from CSV response in WSO2

I am new to WSO2. I wrote Custom Java Class Mediator to transform JSON request to CSV format. I have a proxy service to SFTP the generated CSV file to a (MoveIT) folder.
Custom Mediator converts the JSON request properly to CSV format. But, when send the CSV file using transport.vfs.replyfilename to the endpoint, I see the 'text' wrapper as below in the CSV file:
{"text":"1,F20175_A.CSV,20200623135039\n2,123456789,2017-MO-BX-0048,123456789,987654321,Y/N,C/A,20190101,20201231,20190930,75000,44475.86,15563.52,0.00,15563.52,0.00,60039.38,14960.62,60039.38,0.00,20191218\n3,1,999999999\n"}
I set contentType, MessageType properties "text/plain". I also used vfs.ContentType to set to text/plain as below:
text/plain
I know I am missing something. Has anybody come across this issue in WSO EI 6.6? Should I go ahead and write Custom Message Formatter? Any tips?
I want the output to be written as:
1,F20169_A.CSV,20200617153638
2,123456789,2017-MO-BX-0048,123456789,987654321,Y/N,C/A,20190101,20201231,20190930,75000,44475.86,15563.52,0.00,15563.52,0.00,60039.38,14960.62,60039.38,0.00,20191218
3,1,999999999
Thanks!
It seems that after converting to the CSV format the result is available within a text tag. Therefore using the file connector [1] when you write to the file access the text content using //text() XPath. Please refer to the following sample configuration of inputContent.
<fileconnector.create>
<filePath>{$ctx:filePath}</filePath>
<inputContent>{//text()}</inputContent>
<encoding>{$ctx:encoding}</encoding>
<setTimeout>{$ctx:setTimeout}</setTimeout>
<setPassiveMode>{$ctx:setPassiveMode}</setPassiveMode>
<setSoTimeout>{$ctx:setSoTimeout}</setSoTimeout>
<setUserDirIsRoot>{$ctx:setUserDirIsRoot}</setUserDirIsRoot>
<setStrictHostKeyChecking>{$ctx:setStrictHostKeyChecking}</setStrictHostKeyChecking>
</fileconnector.create>
[1]-https://docs.wso2.com/display/ESBCONNECTORS/Working+with+the+File+Connector#WorkingwiththeFileConnector-create

Azure Logic Apps: Read XML from file and Write a JSON with data

I'm new in it and trying to understand Azure Logic Apps.
I would like to create a LogicApp that:
Looks for new XML-Files
and for each file:
Read the XML
Check if Node "attachment" is present
and for each Attachment:
Read the Filename
Get the File from FTP and do BASE64-encoding
End for each Attachment.
Write JSON File (I have a schema)
DO HTTP-Post to API with JSON file as "application/json"
Is this possible with the Logic-Apps?
Yes, you can.
Check if a node is present, with xpath expression (e.g. xpath(xml(item()),'string(//Part/#ref)'))
For Get File from FTP, use the action FTP - Get File Content
Write JSON File, use the action Data Operations - Compose. If you need transformations, you have to use an Integration Account and Maps.
Do HTTP Post to API, use de action HTTP

Convert and Transform JSON HTTP request to XML

I need to create a Logic Apps workflow with three steps:
When HTTP Request is received (JSON)
Convert Json from request to XML
Save XML file to FTP
What I have done so far:
Add action "When HTTP Request is received"
Add Liquid to Convert JSON to XML
(but i don't see option JSON to XML...Only Tranform JSON to JSON, JSON to
TEXT, XML to JSON, XML to TEXT)
Add action "FTP - Create file"
I also created Integration Account and try to add map for mapping JSON to XML, but I can't find any examples/templates to do this...
Is it possible at all ? Maybe there is another way to convert between these two formats ?
When you just want to convert a JSON payload to an XML file, without doing any transformation to the data, you can use the built-in xml() function of the Workflow Definition Language.
Detailed info in the docs: Workflow Definition Language reference #xml
I've made a small test Logic App to demo your usecase. It looks like this:
As you can see I use the xml function on the triggerbody #xml(triggerBody()) as an input for my FTP file content.
Remark: This will only work if your JSON message has a single rootnode. Otherwise the xml conversion will fail. You'll get this error:
The provided value cannot be converted to XML: 'JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName.
You can work around that by concatenating a rootnode to your JSON payload. The function then would look like: #xml(json(concat('{\"rootnode\":',triggerBody(),'}')))
Good luck testing this out. Let me know if you need more help with this.

How to omit fields when using camel-xmljson JAR to convert XML string to JSON string

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setEncoding("UTF-8");
xmlJsonFormat.setForceTopLevelObject(true);
xmlJsonFormat.setTrimSpaces(true);
xmlJsonFormat.setSkipNamespaces(true);
xmlJsonFormat.setRemoveNamespacePrefixes(true);
Pool a XML file from source directory and transform it to JSON format before sending it to destination directory
from("file:/inn?noop=true").marshal(xmlJsonFormat)
.setHeader(Exchange.FILE_NAME, simple("${file:onlyname.noext}.json"))
.to("file:/out").end();
It works great when i want a JSON string right from a XML without using a POJO.
But now consider a XML where I would like to omit an element.
Should I use another lib but camel-xmljson?
Is this way the right way of accomplish it:
JAXB lib to get a POJO from XML
Jackson lib to get a JSON from POJO and with excluding POJO fields from marshalling
I understand, even I would try to avoid any POJO's in camel routes.
Having said that, I wont just straight way let the XML out. I would put a XSLT after the xmljson component, so that I can trim the xml for desired output.
Thanks gnanagurus! This is quite simple using XSLT. Moreover the xslt: component is included in the last version of the core of Camel.
I defined an xlst file 'transform.xsl' taht includes the field that is to be omited. And then defined the Camel route like that:
from("file:/inn?noop=true")
.to("xslt:file:src/transform.xsl")
.marshal(xmlJsonFormat)
.setHeader(Exchange.FILE_NAME, simple("${file:onlyname.noext}.json"))
.to("file:/out").end();

How do I validate JSON against XML Schema (XSD) or RelaxNG?

I'm defining the structure of JSON documents. I'd like to know if how to validate the JSON documents against existing XSD, RelaxNG schemas or another standard schema language schema. I know of Jsonix, but I don't see that it uses the regular expressions from XSD or RelaxNG for validation against JSON schema (and I don't think that JSON schema is standardized).
Clarification: We already have existing XML and XSD. We can always go back to XML for validation, but it would be cool if we could validate the JSON directly, and would give us more confidence when we want to use the JSON and not XML.
Update: Here is the specification in question: http://www.web3d.org/specifications/x3d-3.4.xsd note that it doesn't have text nodes in the XML documents.
Preliminary answer (still a work in progress, but you can contribute):
If you want to convert XML schema to JSON schema, try downloading: XSD2OWL stylesheet which converts XML Schema to OWL. I converted my schema to owl like this:
$ xmlsh
$ xslt -f xsd2owl.xsl -cf file.xsd > file.owl
$ exit
Then download owl2jsonschema.js ** NO LICENSE ** and modify it until it until the demo works. The output will be in demo/OUTPUT/schema folder/*.json as separate JSON files.
XSD and RelaxNG are defined against XML, not JSON.
For JSON, see JSON Schema, but realize that it has nowhere near the adoption of XSD, and the latest draft of the specification expired August 3, 2013, casting doubts on the future of the effort.
Update
How do I validate JSON against XML Schema (XSD) or RelaxNG?
You don't.
The question is not "Can I?" but "How?" Say I have total control over
the JSON document.
When the answer to "Can I?" is "No" the question of how does not apply.
Clarification: We already have existing XML and XSD. We can always go
back to XML for validation, but it would be cool if we could validate
the JSON directly, and would give us more confidence when we want to
use the JSON and not XML.
You can validate the JSON directly against a JSON Schema, but not against an XSD. There are no tools that can do that; the standards are substantially different. The need to define standard vocabularies and grammars that is served by XSD and RelaxNG against XML was intended to be met by JSON Schema against JSON.
You're looking for "confidence when we want to use the JSON and not XML" in the wrong place. See reasons for choosing XML vs JSON instead.
As you're probably already aware of the information I'm about to post, this is just for reference.
Jsonix Schema Compiler supports generation of JSON Schema based on the XML Schema.
So with this you can convert you XML Schema into JSON Schema and validate your JSON against this JSON Schema using AJV.
This is still an experimental feature but that's the direction.