Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Hello Everyone,

The much-awaited JSON parse feature in SAP Cloud Application Studio (for SAP ByD and C4C) is here. Partners can now parse JSON using ABSL.

To parse JSON in ABSL, we faced a lot of difficulties and used string manipulation functions. But now we can use the new libraries to parse JSON format without any difficulties.

In version 2005 of the SAP Cloud Applications Studio, the following Reuse Library functions are new to JSON:


  1. Json.IsValidJson(json_string) : Returns true for valid JSON




  2. Json.GetArrayLength(keys, json_string): Returns the total array length for the keys passed.



  3. Json.ParseKeyValues(keys, json_string): Parses the JSON and provides value for the given keys.


To use this library and its functions, you must import the AP.PDI.Utilities namespace in ABSL script.

Sample JSON Format: 




1. Validation JSON format


This function allows you to check if the JSON string is valid or not. The output is a boolean value where True indicates valid JSON string and False indicates Invalid.

Syntax:
Json.IsValidJson(json_string)

 

Sample Code:
import ABSL;
import AP.PDI.Utilities;

var JsonString = "{\"d\": {\"results\": [{ \"__metadata\": { \"type\": \"EmployeeDetails.Employee\" }, \"UserID\": \"E12012\",\"RoleCode\": \"35\"}]}}";
var Result = Json.IsValidJson(JsonString);

 

2. Parse JSON KeyValues


This function allows us to get values for the desired key present in the JSON string. We can parse JSON using Json.ParseKeyValues() function.

The following parameters need to be filled:


  • KeysTable: Table consisting of keys for which value is required.




  • JsonStringJSON string consisting the keys passed




After executing the API, the function returns the following parameter in the resulting structure.


Result: A table consisting of the following columns:


  • Key: Key passed in the importing parameter.




  • Value: Value obtained for the key passed.




  • Error: Flag indicating an error; True if there is an error, else False.




  • Message: Information on why the error occurred (for example, Invalid key [Key_Name], if an invalid key is passed).




Syntax:
Json.ParseKeyValues(keys, json_string)

Sample Code:
import AP.PDI.Utilities;
import AP.Common.GDT;

var Result : JsonResult;
var keys : collectionof LANGUAGEINDEPENDENT_EXTENDED_Text;
var key ;

key = "d.results[1].UserID";//full path of key needs to be specified here
keys.Add(key);

key = "d.results[1].RoleCode";//full path of key needs to be specified here
keys.Add(key);

var JsonString = "{\"d\": {\"results\": [{ \"__metadata\": { \"type\": \"EmployeeDetails.Employee\" }, \"UserID\": \"E12012\",\"RoleCode\": \"35\"}]}}";
Result = Json.ParseKeyValues(keys, JsonString);

foreach(var res in Result.KeyValue)
{
var value = res.Value;
}



 

3. Get Array length of array present in JSON.


This function allows you to get the length of array present in JSON string. After executing the API, the function returns the following parameter in the resulting structure.

Result: A table consisting of the following columns:


  • Key: Key passed in the importing parameter.




  • Length: Numeric value representing array length; -1 is sent if any error occurs or passed key is not an array.




Syntax:
Json.GetArrayLength(keys, json_string)

Sample Code:
import AP.PDI.Utilities;
import AP.Common.GDT;

var Result : JsonArrayLength;
var keys : collectionof LANGUAGEINDEPENDENT_Text;
var key ;
key = "d.results";//valid key
keys.Add(key);

var JsonString = "{\"d\": {\"results\": [{ \"__metadata\": { \"type\": \"EmployeeDetails.Employee\" }, \"UserID\": \"E12012\",\"RoleCode\": \"35\"}]}}";
Result = Json.GetArrayLength (keys, JsonString);

foreach(var res in Result.Arraylength)
{
var value = res.Length;
}

 

Note: Full path of key needs to be specified.  don't use Key names to find the length. 

Advantages:-


  • JSON reuse library functions are very useful to parse JSON format.



  • No need for manual string manipulations to parse JSON anymore. Standard reuse functions can do that for us without any difficulties.


For example, you are using REST/OData web service and want to consume the JSON format using ABSL Script in SAP Cloud Application Studio. You can use the above-mentioned function to complete the development.

 

For more details please refer to SAP help documentation – JSON (Reuse Library) or   JSON Parse Help Document

 

I hope this new reuse function saves some effort for SDK developers out there.

 


Best Regards,

Anantharaj Sivalingam

22 Comments
Labels in this area