Skip to Content
Technical Articles
Author's profile photo Piotr Kurzynoga

Using the JSON Reuse Library in SAP Cloud Applications Studio

Hello Everyone,

In 2005 a new JSON Reuse Library was introduced which offers the following functionalities in relation to handling JSON Data:

  • Validation of JSON data
  • Parsing the values of the key
  • Retrieving the length of the array within the JSON structure

 

Key Formatting

To be sure that the key we are accessing returns the correct value, there are two important rules to follow when formatting the key.

  • Array within an Object

If we have an object which contains an array we need to pass the object name followed by the index of the array.

In the following example we want to access Item2 of the ObjectA array:

{ObjectA:["Item1","Item2"]}

To access Item2 we pass the following key: ObjectA[2]

  • Object within an Array

If the data set begins with an array, the index has to be passed and then subsequent objects have to be accessed.

In the example below we will access an object within an array:

[{ObjectA:["Item1","Item2"]},{ObjectB:["Item3"]}]

To access Item2 we pass the following key: [1].ObjectA[2]

To access Item3 we pass the following key: [2].ObjectB[1]

 

The above logic can be used to address any Array/Object combination in a JSON data set.

Important:

Commonly in programming languages such as Java or C#, an index starts with 0. In the case of this Reuse Library the index starts with 1.

 

JSON Data Set

In this example we will use the following data set which represents an array of objects. Within this array of objects there is a nested array of objects (“Location”) as seen below.

[{
		"Name": "John",
		"Gender": "Male",
		"Age": 30,
		"Location": [{
				"City": "Galway",
				"Country": "Ireland"
			}
		]
	}, {
		"Name": "Jane",
		"Gender": "Male",
		"Age": 25,
		"Location": [{
				"City": "Dublin",
				"Country": "Ireland"
			}
		]
	}, {
		"Name": "Peter",
		"Gender": "Male",
		"Age": 45,
		"Location": [{
				"City": "London",
				"Country": "England"
			}
		]
	}
]

 

ABSL Coding

Below we have a custom action created on a custom business object that will be used to parse our JSON data.

Note:

In a business scenario, the resulting JSON data set would be returned from a REST API Endpoint.

However in this example we will directly pass our data set into the SAP Cloud Applications Studio as “TestString”.

import ABSL;
import AP.PDI.Utilities; //This Library contains the JSON Functions
import AP.Common.GDT; //Required for the keys collection

//This is our example JSON Array of Objects which includes a
// nested Array of Objects ("Location")
var TestString = "[{\"Name\":\"John\",\"Gender\":\"Male\",\"Age\":30,\"Location\":[{\"City\":\"Galway\",\"Country\":\"Ireland\"}]},{\"Name\":\"Jane\",\"Gender\":\"Male\",\"Age\":25,\"Location\":[{\"City\":\"Dublin\",\"Country\":\"Ireland\"}]}";
TestString=TestString.Concatenate(",{\"Name\":\"Peter\",\"Gender\":\"Male\",\"Age\":45,\"Location\":[{\"City\":\"London\",\"Country\":\"England\"}]}]");

//Let's check if the JSON Passed is valid
var CheckValidity = Json.IsValidJson(TestString);

//If the JSON is valid let's retrieve our keys.
if(CheckValidity) {
    //This collection will hold the keys of interest
	var keys : collectionof LANGUAGEINDEPENDENT_Text;
	//This will be our placeholder for the key
    var key; 

	key = "[1].Name";
	keys.Add(key);

	key = "[1].Age";
	keys.Add(key);

	key = "[1].Location[1].City";
	keys.Add(key);

	key = "[3].Location";
	keys.Add(key);
	
        //Let's retrieve the key values
	var keyResult = Json.ParseKeyValues(keys, TestString);
        //Let's retrieve the length of the arrays
	var jsonLength=Json.GetArrayLength(keys, TestString);
}

 

 

Explanation and Debugging of the JSON Reuse Library Functions in ABSL

  • IsValidJson

The IsValidJson function returns true if the passed JSON data set is in the correct JSON format, otherwise false is returned.

Here we can see the returned value is true which means the JSON data set is correctly formatted.

  • ParseKeyValues

Important:

Key Values are returned in the form of an array which starts with index 0 and not with index 1 as the Reuse Library.

The ParseKeyValues function provides the following output as seen below:

For each key passed we can see its corresponding value, in case of the fourth key “[3].Location” the index of the Location was not passed hence an error is thrown stating “Index expected for key Location”.

  • GetArrayLengths

The fourth key is however useful in the next step where we retrieve the Array Lengths by using the GetArrayLengths function as visible below:

If the passed key is incorrect or is not an array -1 is returned, as the first 3 keys passed here directly point to values, -1 is returned. For the fourth key we are passing the Location Array which returns the Length of 1, this means that the array contains 1 value.

 

Conclusion

You can also find more information about Reuse Libraries in the SAP Cloud Applications Studio Documentation which is available in the SAP Help Portal.

If you have any further queries or feedback regarding the JSON Reuse Library please comment below!

Best Regards,

Piotr Kurzynoga.

Assigned tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Anant Acharya
      Anant Acharya

      Dear Piotr,

      Nice blog with detail explanation about JSON Array feature.

      Regards
      Anant

       

       

      Author's profile photo René Nebel
      René Nebel

      Piotr, thank you for your post! In case anyone wondered, key wildcard's seems not possible, e.g.

      key = "[*].Name";

      or

      key = "[1,2].Name";

      and results in a backend dump!

       

      Author's profile photo Piotr Kurzynoga
      Piotr Kurzynoga
      Blog Post Author

      Hi Rene,

      This is a great suggestion,

      Thanks for pointing this out,

      Piotr.

      Author's profile photo Cian Barrett
      Cian Barrett

      Very useful blog Piotr!

      Author's profile photo Saurabh Kabra
      Saurabh Kabra

      Very nice and much-needed explanation about how to use JSON Reuse Library. Thanks!

      Author's profile photo Solène ROQUES
      Solène ROQUES

      Hello all,

       

      Thanks Piotr for this really great post.

      Is there anything similar for xml ? Or are we still obliged to use FindRegEx ?

      Thanks,

      Author's profile photo Piotr Kurzynoga
      Piotr Kurzynoga
      Blog Post Author

      Hello Solene,

      Thank you for the feedback, this feature is currently available for JSON Responses, for XML parsing you still have to use String operations.

      Thanks,

      Piotr.

      Author's profile photo Solène ROQUES
      Solène ROQUES

      Hello Piotr,

       

      Could you provide an exemple with several instances of the subnode please ? For example, Peter has an adress in London and another in Dublin, and we want to retrieve both adresses, not only the first one (or any other example, it does not matter...)

      I assume, there will be a loop and something like Location[i].City but I will be happy to see the exact synthax.

      Thanks for your help

      Best regards