Skip to Content
Personal Insights
Author's profile photo Lars Hvam

Editing JSON embedded in ABAP strings

I have many unit tests which takes JSON as input, however editing JSON embedded in a ABAP string is a mess. And no, ecatt test data containers is not an option, never use it for anything in unit tests, but its a different story.

One option is to use string templates,

DATA(json) = |\{\n| &&
  |  "hello": 2\n| &&
  |\}|.

It does require escaping the squiggly brackets, so I tend to do the following instead,

DATA(json) = `{` && |\n| &&
  `  "hello": 2` && |\n| &&
  `}`.

Which is also a mess, but easy to add when using multi cursors.

After adding the JSON, I sometimes need to edit the JSON, and I typically mess it up, so the syntax is wrong or something else 😒

Lets add tooling to easily edit the JSON 😎

 

Introducing abap-json-editor, right click the start of the ABAP string to open an new editor. Changes in the JSON editor is automatically validated and reflected in the ABAP code,

Plus it allows for easy pasting of JSON from clipboard,

 

The extension will work with standalone ABAP files, files from ABAP fs by Marcello Urbani, plus on web only

Immediately released to everyone 😅 bug reports and fixes welcome on github.

 

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Matthew Billingham
      Matthew Billingham

      Excellent!

      Author's profile photo Sandra Rossi
      Sandra Rossi

      If I'm not wrong, there's nothing in Eclipse/ADT to help in this (so nicely), hopefully SAP developers have probably subscribe to your blog posts and will include such a nice feature soon...

      Author's profile photo Lars Hvam
      Lars Hvam
      Blog Post Author

      well, to be fair, I did spend a few hours messing around with the vscode API

      Author's profile photo Fabian Lupa
      Fabian Lupa

      Cool workaround! Hopefully ABAP will get multi line string literals at some point.

      DATA(json) = ```
      {
        "hello": 1
      }
      ```.
      
      DATA(number) = 1.
      DATA(json2) = |||
      \{
        "hello": { number }
      \}
      |||.

      ADT has a feature to escape and line break contents when copy pasting it. But that doesn't help editing or reading anything later.

      How to wrap long strings automatically in ADT | SAP Blogs

       

      Author's profile photo Lars Hvam
      Lars Hvam
      Blog Post Author

      yea, hopefully 🙂

      Author's profile photo Oleg Bashkatov
      Oleg Bashkatov

      Your extension is good! Thank you!

       

      However, for my opinion for unit tests more understandable would be use abap-structures like in the following

      CLASS ltc DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
      
        PUBLIC SECTION.
          METHODS mega_feature_by_json_input FOR TESTING.
      
        PRIVATE SECTION.
          METHODS _get_json4mega RETURNING VALUE(rv_val) TYPE string.
      ENDCLASS.
      
      CLASS ltc IMPLEMENTATION.
        METHOD mega_feature_by_json_input.
          DATA(lv_json_input) = _get_json4mega( ).
        ENDMETHOD.
      
        METHOD _get_json4mega.
          "RETURNING VALUE(rv_val) TYPE string.
          TYPES: BEGIN OF ts_target_fields
                    , hello TYPE string
                    , world TYPE int1
                    , startdate TYPE sydatum
                  , END OF ts_target_fields
                  .
          DATA ls_target_fields TYPE ts_target_fields.
      
          ls_target_fields-hello       = 'Data Dictionary More than just structure'    .
          ls_target_fields-world       = '42'.
          ls_target_fields-startdate   = '19720401'.
      
          rv_val =
          /ui2/cl_json=>serialize(  EXPORTING  data        =   ls_target_fields
                                               pretty_name = /ui2/cl_json=>pretty_mode-low_case  ).
        ENDMETHOD.
      ENDCLASS.
      Author's profile photo Lars Hvam
      Lars Hvam
      Blog Post Author

      yea, its just a different kind of test

      if the team knows JSON, and is used to transfer JSON, its a different world to know the ABAP language. If the input to the logic is JSON then I recommend testing it as JSON

      Author's profile photo Chad He
      Chad He

      Cool feature.

      And some questions.

      I'd like to know, did you often use VS Code to edit ABAP?

      Or just some specific scenarios that like JSON-EDIT?

      In your opinion, is VS Code better than ADT when edit abap except CDS?

      Author's profile photo Lars Hvam
      Lars Hvam
      Blog Post Author

      I'd say I spend 80->90% of my time developing ABAP in vscode, in a standalone setup editing just files -> pushing -> git -> then pulling into the system with abapGit.

      The rest of the time I use SE24/SE80, I dont do very much CDS.