9

XML To JSON conversion on SAP CI and JSON Conventions and why they are so Import...

 1 year ago
source link: https://blogs.sap.com/2023/01/28/xml-to-json-conversion-on-sap-ci-and-json-conventions-and-why-they-are-so-important/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction

We all have come across scenarios where while Integrating between SAP and third party systems we have to convert from

JSON to XML – So that SAP understands the JSON data being sent by the third party

or from

XML to JSON -so that the third party system that understands(or prefers to Understand) only JSON can consume the XML data being sent by SAP.

You should not do these conversions without knowing the supported JSON Conventions by the third party system and then comparing it with SAP CI standard JSON convention as it would be as ridiculous as generating a XSD out of an XML via same online tool.

SAP CI XML to JSON convertor by default supports Rabbit Fish Convention while most of the platforms accept Badger Fish JSON Convention.

Overview

Let’s understand and educate ourselves of the differences between the various JSON conventions.

There are 4 major JSON conventions

  1. Badger Fish – Most popular but memory intensive in terms of storage
  2. Rabbit Fish – Best representation of fields and Attributes
  3. Ray Fish
  4. Plain JSON

To understand the difference let’s have a look at some examples

JSON Convention JSON Representation XML Data
Badger Fish
{
	"FirstLevel": {
		"@Source": "InService",
		"Task": {
			"@nodeAttribute": 234,
			"JobNumber": {
				"$": "J918290"
			},
			"JobDescription": {
				"$": "This is a temp job"
			},
			"LineOfBusiness": {
				"$": "Supply Restoration"
			},
			"TaskType": {
				"@elementAttribute": "eA",
				"$": "Miscellaneous"
			}
		}
	}
}
<FirstLevel Source="InService">
	<Task nodeAttribute="234">
		<JobNumber>J918290</JobNumber>
		<JobDescription>This is a temp job</JobDescription>
		<LineOfBusiness>Supply Restoration</LineOfBusiness>
		<TaskType elementAttribute ="eA">Miscellaneous</TaskType>
		
	</Task>
</FirstLevel>
Rabbit Fish
{
	"FirstLevel": {
		"@Source": "InService",
		"Task": {
			"@nodeAttribute": 234,
			"JobNumber": "J918290",
			"JobDescription": "This is a temp job",
			"LineOfBusiness": "Supply Restoration",
			"TaskType": {
				"@elementAttribute": "eA",
				"$": "Miscellaneous"
			}
		}
	}
}​
<FirstLevel Source="InService">
	<Task nodeAttribute="234">
		<JobNumber>J918290</JobNumber>
		<JobDescription>This is a temp job</JobDescription>
		<LineOfBusiness>Supply Restoration</LineOfBusiness>
		<TaskType elementAttribute ="eA">Miscellaneous</TaskType>
		
	</Task>
</FirstLevel>
Ray Fish
{
	"#name": "FirstLevel",
	"#text": null,
	"#children": [
		{
			"#name": "@Source",
			"#text": "InService",
			"#children": []
		},
		{
			"#name": "Task",
			"#text": null,
			"#children": [
				{
					"#name": "@nodeAttribute",
					"#text": 234,
					"#children": []
				},
				{
					"#name": "JobNumber",
					"#text": "J918290",
					"#children": []
				},
				{
					"#name": "JobDescription",
					"#text": "This is a temp job",
					"#children": []
				},
				{
					"#name": "LineOfBusiness",
					"#text": "Supply Restoration",
					"#children": []
				},
				{
					"#name": "TaskType",
					"#text": "Miscellaneous",
					"#children": [
						{
							"#name": "@elementAttribute",
							"#text": "eA",
							"#children": []
						}
					]
				}
			]
		}
	]
}
<FirstLevel Source="InService">
	<Task nodeAttribute="234">
		<JobNumber>J918290</JobNumber>
		<JobDescription>This is a temp job</JobDescription>
		<LineOfBusiness>Supply Restoration</LineOfBusiness>
		<TaskType elementAttribute ="eA">Miscellaneous</TaskType>
		
	</Task>
</FirstLevel>
Plain JSON
{
	"FirstLevel": {
		"Source": "InService",
		"Task": {
			"nodeAttribute": 234,
			"JobNumber": "J918290",
			"JobDescription": "This is a temp job",
			"LineOfBusiness": "Supply Restoration",
			"TaskType": {
				"elementAttribute": "eA",
				"$": "Miscellaneous"
			}
		}
	}
}​
<FirstLevel Source="InService">
	<Task nodeAttribute="234">
		<JobNumber>J918290</JobNumber>
		<JobDescription>This is a temp job</JobDescription>
		<LineOfBusiness>Supply Restoration</LineOfBusiness>
		<TaskType elementAttribute ="eA">Miscellaneous</TaskType>
		
	</Task>
</FirstLevel>

As you can see Rabbit Fish is the most meaningful and complete representation of attributes and nodes. The only possible way to achieve selective JSON to XML conversion is via XSLT mapping (for formats other than RabbitFish) as SAP CI only supports RabbitFish at the moment I hope that we should get radio buttons in the UI to chose the Convention soon.

PS: I have no idea why all the JSON Conventions end in the word “Fish”.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK