# JSON Schemas with Semantic Annotations Supporting Data Translation
## 6 Translating JSON to XML and XML to JSON
### Translating XML to JSON  
This section proposes an algorithm for the translation of an XML instance to a JSON instance using XSLT, based on the elements data types defined in its schema. To correctly output the data types of the JSON instance properties, the algorithm receives a translated JSON schema as an XSD to create a JSON instance compliant to the data types representedin it, since XML instances do not carry data type definitions. The usage of the XML schema instead of the JSON schema is due to the accessibility in XSLT through Xpath expressions to the element data type. In XML, the datatype of an element/attribute is only represented in its schema, with the XML node values being represented as they are, without any differentiation between strings, integers, floats, or any data type. In JSON, it is different; strings are represented within quotation marks while other data types are not. Without this extra information provided by the XML Schema, the translator has no way of knowing if the JSON property it is translating is a string or any other datatype. This will influence the validation of the JSON message according to its JSON schema; for example, it might be expecting a number as 14, and it receives one as \"14\", which will not be valid. The proposed algorithm is presented in Listing 10. Similar to the algorithm of Section 6.1, this was developed for implementation in XSLT. Initially, the XML document root node is loaded and an empty JSON document is created, whereas the root brackets are opened and closed before and after a recursive function \(create  
```
1 LOAD to XMLSchema the XML schema
2 LOAD to XMLSchema the XML. file
3 CREATE empty JSON
4 Open curly bracket
5 IF currentNode.name=!=\"root\"THEN
6 Write element name and open curly bracket( \"currentNode.name\":[ )
7 CAL created/SON(OMIndoc.child, XMLSchema)
8 Close curly bracket
9 ELSE
10 CAL create]SON(OMIndoc.child, XMLSchema)
11 END IF
12 Close curly bracket
13
14 FUNCTION creates/SON(currentNode, XMLSchema)
15 IF currentNode.name equals preceding sibling and differs following sibiling THEN
16 CAL.writeValueOrderObject(currentNode, XMLSchema)
17 Close square brackets
18 IF currentNode has following sibiling THEN
19 Write a comma
20 CAL create]SON (currentNode.following Sibiling)
21 BD IF
22 ElSE IF currentNode.name equals preceding sibling THEN
23 CAL writeValueOrderObject(currentNode, XMLSchema)
24 Write a comma
25 ElSE IF currentNode.name equals following sibiling THEN
26 Write element name and open square bracket( \"currentNode.name\":[ )
27 CAL writeValueOrderObject(currentNode, XMLSchema)
28 Write a comma
29 ElSE IF currentNode has child elements THEN
30 Write element name and open curly bracket( \"currentNode.name\":[ )
31 ROME4 currentlyNode.child
32 CALL create]SON (currentNode.child)
33 END FOR
34 ElSE IF currentNode has no child elements THEN
35 Write element name
36 Evaluate XMLSchema for element data type
37 Write element node value in JSON with or without quotes according to data type
38 IF currentNode has following siblings THEN
39 Write a comma
40 CAL create]SON (currentNode.following Sibiling)
41 ElSE
42 Close curly brackets
43 END IF
44 END IF
45 END-FUNCTION
46
47 FUNCTION writeValueOrderObject(currentNode, XMLSchema)
48 IF currentNode has child THEN
49 Open curly brackets
50 CALL create]SON (currentNode.child, XMLSchema)
51 ELSE
52 Evaluate XMLSchema for element data type
53 Write element node value in JSON with or without quotes according to data type
54 END IF
55 END FUNCTION
```
**Listing 10** XMLSchema the XML schema  
For demonstrating the results of this translator, we can use all of the previously given examples in reverse order. The original XML document is presented on Listing 9. This algorithm is applied to it while getting the data types presented on the translated XML schema of Listing 6, achieving the translated JSON instance in Listing 1, which is valid according to its JSON schema of Listing 4. ```





integrity and core functions.