Convert XSD to JSON Schema - json

My application support XML and JSON content type. I have XSD to validate XML content. I want to convert XSD to create a JSON Schema. Can I achieve this functionality in using C# ?

There is a commercial .NET library XsdToJsonSchema that converts from XSD to JSON schema that seems to be close to what you are looking for. A code snippet from its page:
string xsd = File.ReadAllText("XMLFile1.xsd");
string jsonSchema = XsdToJsonSchema.XsdToJsonSchemaConverter.ConvertXsdToJsonSchema(xsd);

Related

Converting JSON to XMl file

What will be the preferred way of converting JSON to XML file? Is it better to use web-service or any Java component? I tried using web-service and still wanted to cross-check if there is any other option to do this.
Convert JSON text contained in string json into an XML node
XmlDocument Xmldoc = JsonConvert.DeserializeXmlNode(json);
Here is the documentation:
Json to xml

Generate Angular2 forms from Swagger API specification

I'm looking for a way to generate a set of Angular2 form templates from a Swagger API definition file. I want a result that will allow me to test my POST/PUT requests, and even use it in my app.
After some research I found this Angular2 form library that takes a JSON schema as input: https://github.com/makinacorpus/angular2-schema-form
So if you know of a Swagger -> JSON Schema converter that will work too.
Cheers!
So if you know of a Swagger -> JSON Schema converter that will work
too.
Swagger 2.0 supports a subset of JSON schema draft 4. This is what swagger's Schema object is. From the docs:
The following properties are taken directly from the JSON Schema
definition and follow the same specifications:
$ref - As a JSON Reference
format (See Data Type Formats for further details)
title
description (GFM syntax can be used for rich text representation)
default (Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object)
multipleOf
...
The following properties are taken from the JSON Schema definition but
their definitions were adjusted to the Swagger Specification.
items
allOf
properties
additionalProperties
It should be a fairly simple exercise to manually extract the schema from your swagger, but I don't know of any automated tool to do this. I think the fact some of the JSON schema properties have been modified by swagger may make auto conversion problematic in certain circumstances.

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.

how to convert XML to JSON in angularjs?

I want to load an xml file and convert it into JSON in angularjs on the client side.
You have two options.
Return the data from the API in format you require to prevent conversions (recommended).
Convert the XML to JSON using javascript.
For approach #2 I'll recommend you this solution http://goessner.net/download/prj/jsonxml/
And please be sure to read the article 'Converting Between XML and JSON' (by Stefan Goessner) to overview details of the problems with conversions.