cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CAI - storing json array using script

david_deeg
Participant
0 Kudos

Hello,

I have the following challenge:

I have a JSON file stored in the memory with the name "API_result". The stucture looks something like this:

"memory": {
      "API_result": [
        {
          "value A": "aaaaa",
          "componentId": "000000000019981475",
          "value B": "bbbbb",
          "value C": "CCCCC"
       }
        {
          "value A": "aaaaa",
          "componentId": "000000000003136619",
          "value B": "bbbbb",
          "value C": "CCCCC"
        }

       {
          "value A": "aaaaa",
          "componentId": "000000000003121415",
          "value B": "bbbbb",
          "value C": "CCCCC"
        }
        ]
}

I want to store the values of component ID in a separate memory (memory.instrument) as JSON, something like this:

"memory": {
      "instrument": [
        {
          "c_Id": "000000000019981475",
       }
        {
          "c_Id": "000000000003136619",
        }

       {
          "c_Id": "000000000003121415",
        }
        ]
}

To do this, I used the action "update memory" -> "set memory" with the option "Use scripting to build my JSON" enabled. I use following code (based on the documentation)

{{#eachJoin memory.API_result}}
    {  
      "c_ID": {{componentId}}
    }
  {{/eachJoin}}

This code causes a warning and those not store anything in memory.instrument.

 {
        "level": "warning",
        "code": "W_EXECUTE_ACTION",
        "data": {
          "type": "edit_memory",
          "value": {
            "invalid_json": "    {\r\n      \r\n      \"c_ID\": 000000000019981475\r\n      \r\n    }\r\n,    {\r\n      \r\n      \"c_ID\": 000000000003136619\r\n      \r\n    }\r\n,    {\r\n      \r\n      \"c_ID\": 000000000003121415\r\n      \r\n    }\r\n,    
            "scripting_enabled": true
          },
          "message": "No valid JSON value for field instrument"

I tried some other variations but had no luck. Any hint is highly welcome.

Accepted Solutions (1)

Accepted Solutions (1)

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

I was able to do it but you have to put array code outside. Also, assuming you want it to continue to be a string, you have to put quotes around the component ID.

[
{{#eachJoin memory.API_result}}
    {  
      "c_ID": "{{componentId}}"
    }
{{/eachJoin}}
]

david_deeg
Participant
0 Kudos

Thanks a lot, I tried this but i did not work. Now I just copied your code and it does. Seems like I had a blank or something in my code.

Answers (0)