Skip to Content
Technical Articles

Parse JSON format in ABSL using Cloud Application Studio

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

/
7 Comments
You must be Logged on to comment or reply to a post.
  • Hey Folks,

    does anyone else have an issue when adding more than 50 keys to the table???

    GetArrayLength() as well as ParseKeyValues() is returning an empty table then. I double checked my JSON string. Key strings are ok, JSON string is correct.

    Regards

    Christian

     

    • Hi Christian,

      Same issue for me.

      I’m fetching an array of project records from external source via REST call, and want to parse three field values per array entry from the result.

      With a page size of 10 (-> 30 key values) it is working fine. But with a page size of 20 (-> 60 key values) the result of Json.ParseKeyValues is empty.

      I can parse the key values one by one per project record or in packages of 10, but this seems to be quite some runtime overhead.

      Source Code:

      var result = WebServiceUtilities.ExecuteRESTServiceWithoutEncoding(scenarioName, serviceName, httpMethod, httpResource, URLParameterList, headerParameterList, contentType, body);
      var json = “{\”projects\”: ” + result.Content + “}”;
      var keys : collectionof LANGUAGEINDEPENDENT_EXTENDED_Text;
      var key;

      //Determine JSON Array Length
      key = “projects”;
      keys.Add(key);
      var L = Json.GetArrayLength(keys, json).Arraylength.GetFirst().Length;

      var i = 1;
      keys.Clear();
      while (i <= L)
      {
      key = “projects[” + i.ToString() + “].PROJECTID”;
      keys.Add(key);
      key = “projects[” + i.ToString() + “].VALUE”;
      keys.Add(key);
      key = “projects[” + i.ToString() + “].TIME_STAMP”;
      keys.Add(key);
      i = i + 1;
      }
      var jsonResult = Json.ParseKeyValues(keys,json);

      • I opened an incident and SAP stated that the functions are in fact limited due to performance reasons.

        My solution has several hundreds of records with 10 fields (at this very moment). I implemented a workaround that should also work for your example.

        Just put another a keys.Clear() and also the ParseKeyValues function into your loop. You need to parse each record separately. Then you could write your jsonResult to a collection (or as I did to my custom “Record” BO and then to a Collection of Records)

        As the records in my solution will have up to 150 fields, it will become very ugly to implement that. So I will open a case on influence.sap.com (and post the link here). Hopefully they will raise the limit 🙂