Wiki source code of Transformation - XML to JSON
Last modified by Danniar Firdausy on 2024/09/05 10:29
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{container}} | ||
2 | {{container layoutStyle="columns"}} | ||
3 | ((( | ||
4 | In this microlearning session, we’ll dive into transforming XML into JSON using eMagiz. We’ll guide you through the process of ensuring that your XML input is correctly converted into a valid JSON output by utilizing the tools available in eMagiz, such as default message formats, data models, and message mapping. By the end of this session, you’ll be equipped with the knowledge to perform these transformations efficiently within the Design and Create phases of eMagiz. | ||
5 | |||
6 | Should you have any questions, please contact [[academy@emagiz.com>>mailto:academy@emagiz.com]]. | ||
7 | |||
8 | == 1. Prerequisites == | ||
9 | |||
10 | * Basic knowledge of the eMagiz platform | ||
11 | |||
12 | == 2. Key concepts == | ||
13 | |||
14 | This microlearning centers around transforming XML to JSON | ||
15 | By transforming we mean: Making sure that the input message is transformed in a valid output message by defining how this should happen | ||
16 | |||
17 | eMagiz natively supports the transformation of XML to JSON with the help of: | ||
18 | |||
19 | * Default message format | ||
20 | * Data models in Design | ||
21 | * Message mapping in Design | ||
22 | * Create transformation in Create | ||
23 | * Support Objects | ||
24 | |||
25 | == 3. Transformation - XML to JSON == | ||
26 | |||
27 | In this microlearning, we will focus on transforming XML to JSON within the tooling of eMagiz. With the help of this microlearning, you will know how to transform from XML to JSON with the help of the tooling in Design and Create. To illustrate the functionality we define what needs to be done in various phases of eMagiz. | ||
28 | |||
29 | eMagiz natively supports the transformation of XML to JSON with the help of: | ||
30 | |||
31 | * Default message format | ||
32 | * Data models in Design | ||
33 | * Message mapping in Design | ||
34 | * Create transformation in Create | ||
35 | * Support Objects | ||
36 | |||
37 | === 3.1 Default message format === | ||
38 | |||
39 | In the Design phase of eMagiz, you have the option on the system level to define the message format. In case a system speaks JSON you should set the value to JSON. If the system speaks XML you should set the value to XML. | ||
40 | |||
41 | [[image:Main.Images.Microlearning.WebHome@novice-create-your-transformations-xml-to-json--default-message-format.png]] | ||
42 | |||
43 | Setting this up correctly will help you tremendously in the Create phase. | ||
44 | |||
45 | === 3.2 Data models in Design === | ||
46 | |||
47 | Just as you are used to you simply create the correct system definition in Design as you would do when you would create a data model for XML message. If you are unsure how to do this please revisit the section on Design in the Crash Course Platform. | ||
48 | |||
49 | One extra thing to take into account when handling JSON is the root entity. Contrary to XML it is not mandatory to name your root entity when handling JSON. This means that when you create your data model you should verify beforehand whether the system to which you want to send your messages has a named root entity or not. | ||
50 | |||
51 | An example of a JSON message where the root entity is not named is shown below as the first example. The second example you see below is an example of a JSON message where the root entity is named. | ||
52 | |||
53 | {{code language="xml"}} | ||
54 | { | ||
55 | {"ID": 1234} | ||
56 | } | ||
57 | |||
58 | {"Root": | ||
59 | {"ID": 1234} | ||
60 | } | ||
61 | {{/code}} | ||
62 | |||
63 | If you need to send a name along with the JSON root in your output message you should create an extra entity in your data model with which your system message definition will start. | ||
64 | |||
65 | [[image:Main.Images.Microlearning.WebHome@novice-create-your-transformations-xml-to-json--system-message-named-root.png]] | ||
66 | |||
67 | Regardless of whether the root of your output JSON message is named you also don't need to forget to define your message format. This can be done on the system level (in case all communication happens in JSON) or it can happen on the system message level. On the system message level, you have a button called Change message format which you can press while being in "Start Editing Mode". This action will show a pop-up. Here you can switch between XML and JSON as the message format. Ensure that the message format is JSON | ||
68 | |||
69 | [[image:Main.Images.Microlearning.WebHome@novice-create-your-transformations-xml-to-json--change-message-format.png]] | ||
70 | |||
71 | === 3.3 Message mapping in Design === | ||
72 | |||
73 | Just as you are used to you simply create the correct message mapping in Design as you would do when you would create a message mapping for XML messages. If you are unsure how to do this please revisit the section on Design in the Crash Course Platform. | ||
74 | |||
75 | === 3.4 Create transformation in Create === | ||
76 | |||
77 | Just as you are used to you simply create the correct transformation functionality (filter, transformation, static input, etc.) as you would do when you would create the correct transformation functionality for XML messages. If you are unsure how to do this please revisit the section on Create in the Crash Course Platform. | ||
78 | |||
79 | === 3.5 Needed support objects === | ||
80 | |||
81 | However, eMagiz needs to be told whether the input and/or output message is JSON. To tell this to the flow component you will need some support objects. If you have defined your default message format correctly (i.e. set it to JSON) eMagiz will auto-generate the relevant support objects for you. | ||
82 | |||
83 | Whether or not eMagiz has auto-generated the components the remainder of this section is interesting to take knowledge of. The crucial part of this is the virtual root. Remember we told you to create an extra entity in Design in your system message when the root of the JSON had a name? This is the reason why. As JSON can in theory have multiple roots which XML cannot have, yet we use the same underlying technology in eMagiz to transform both of them (JSON to JSON, JSON to XML, XML to JSON, XML to XML) you need to take this into account. | ||
84 | |||
85 | In this case, we want to transform from XML to JSON and therefore you need a JSON Result factory within your flow. In this component, you need to define your virtual root based on your system message in Design. This is always the name of the root entity as defined as your system message in Design. In the example shown above, this will mean that the virtual root for this example is root. | ||
86 | |||
87 | [[image:Main.Images.Microlearning.WebHome@novice-create-your-transformations-xml-to-json--json-result-factory.png]] | ||
88 | |||
89 | The JSON result factory is needed for your validation. However, to also support the transformation from XML to JSON you need an additional support object called JSON result transformer | ||
90 | |||
91 | [[image:Main.Images.Microlearning.WebHome@novice-create-your-transformations-xml-to-json--json-result-transformer.png]] | ||
92 | |||
93 | Don't forget to properly link the support objects to the correct flow components for them to work. In the offramp, you need to link the support objects to the transformation. In the exit, you need to link the JSON result factory to the validation. | ||
94 | |||
95 | After having done this you can use the unit testing functionality of eMagiz to test your work. If you are unsure how this functionality works please take a look at the segment on unit testing in the Crash Course Platform. | ||
96 | |||
97 | == 4. Key takeaways == | ||
98 | |||
99 | * eMagiz natively supports the transformation of XML to JSON with the help of: | ||
100 | * Default message format | ||
101 | * Data models in Design | ||
102 | * Message mapping in Design | ||
103 | * Create transformation in Create | ||
104 | * Support Objects | ||
105 | * Ensure that you link the support objects correctly | ||
106 | * Don't forget to define the message format | ||
107 | |||
108 | == 5. Suggested Additional Readings == | ||
109 | |||
110 | * [[Crash Courses (Menu)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-design-defining-a-message-format||target="blank"]] | ||
111 | ** [[Crash Course Platform (Navigation)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.WebHome||target="blank"]] | ||
112 | *** [[Defining a message format (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-design-defining-a-message-format||target="blank"]] | ||
113 | *** [[Transformation XML to XML (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-create-transformation-xml-to-xml||target="blank"]] | ||
114 | *** [[Support objects - Introduction (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Crash Course.Crash Course Platform.crashcourse-platform-create-support-objects-introduction||target="blank"]] | ||
115 | * [[Novice (Menu)>>doc:Main.eMagiz Academy.Microlearnings.Novice.WebHome||target="blank"]] | ||
116 | ** [[Create your transformations (Navigation)>>doc:Main.eMagiz Academy.Microlearnings.Novice.Create your transformations.WebHome||target="blank"]] | ||
117 | *** [[Transformation - XML to JSON (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Novice.Create your transformations.novice-create-your-transformations-xml-to-json||target="blank"]] | ||
118 | * [[Intermediate (Menu)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.WebHome||target="blank"]] | ||
119 | ** [[API Management (Navigation)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.API Management.WebHome||target="blank"]] | ||
120 | *** [[Changing the default message format (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.API Management.intermediate-apimanagement-restxml.WebHome||target="blank"]] | ||
121 | * [[Use Cases (Menu)>>doc:Main.eMagiz Academy.Use Cases.WebHome||target="blank"]] | ||
122 | * [[XML to JSON (Search Result)>>url:https://docs.emagiz.com/bin/view/Main/Search?sort=score&sortOrder=desc&highlight=true&facet=true&r=1&f_space_facet=0%2FMain.&f_type=DOCUMENT&f_locale=en&f_locale=&f_locale=en&text=%22xml+to+json%22||target="blank"]] | ||
123 | ))) | ||
124 | |||
125 | |||
126 | ((({{toc/}}))){{/container}} | ||
127 | {{/container}} |