Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member592782
Active Participant
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.
12 Comments
anant_acharya
Advisor
Advisor
Dear Piotr,

Nice blog with detail explanation about JSON Array feature.

Regards
Anant

 

 
RNL
Explorer
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!

 
cianbarrett
Active Participant
Very useful blog Piotr!
former_member592782
Active Participant
0 Kudos

Hi Rene,

This is a great suggestion,

Thanks for pointing this out,

Piotr.

former_member226
Employee
Employee
0 Kudos
Very nice and much-needed explanation about how to use JSON Reuse Library. Thanks!
solene_roques
Participant
0 Kudos
Hello all,

 

Thanks Piotr for this really great post.

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

Thanks,
former_member592782
Active Participant
0 Kudos
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.
solene_roques
Participant
0 Kudos
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
fceylan_hs
Participant
0 Kudos
Hi Piotr,

 

Could you provide a way about how to retrieve object as array?
"Location": [{
"City": "London",
"Country": "England"
}
]

In your example how to get city and country values as an array object?
SuryaAnnepu
Discoverer
0 Kudos
Hi Piotr,

Thanks for writing this wonderful blog on JSON library.

 

One of our API Response structure(screenshot attached below) seems different from usual, and the logic given in the blog seems to be not working.

 

Please suggest how to process this kind of response structures.


 

Thanks in advance,

Surya Kiran
mandeep_shrestha1
Participant
0 Kudos

Thanks for an informative blog fasola55 . I am trying to achieve one requirement of sending the below table as json. Could you please help/guide me with some steps on how should I do it to achieve.

óespinar
Participant
0 Kudos
Hi everyone,

Has anyone encountered time-out problems using this library?

I’m getting the standard "Error in AddOn Y..." email from SAP with dozens of time-outs on this line:
if( Json.IsValidJson( jsData ) == false )

In also on this one with no more than 10 keys:
jsonResult = Json.ParseKeyValues( jsonKeys, response.Body );

This is happening in different tenants and at different times, so I don’t think it’s a performance issue.

In my opinion, the behavior of this library is quite erratic.

Thanks,

Óscar