title: Deserializing JSON xstring to structure in ABAP
body: <p>I would like to deserialize a JSON string to a structure defined like this:</p>
  
  <pre><code>TYPES: BEGIN OF json_subobject,
           c TYPE i,
           d TYPE decfloat34,
         END OF json_subobject.
  TYPES: BEGIN OF json_object,
          a TYPE c LENGTH 10,
          b TYPE json_subobject,
         END OF json_object.
  
  DATA: foo TYPE json_object.
  </code></pre>
  
  <p>I model this structure in JSON like this:</p>
  
  <pre><code>{
    "a":"FooBar",
    "b":{
      "c":9,
      "d":3.14
    }
  }
  </code></pre>
  
  <p>Now, I wrote a simple program to try out the CALL TRANSFORMATION statement on the above JSON accompanied with the above structure definitions. The program should deserialize the JSON (hard-coded into the variable lv_xmls) and print out the contents of the resulting structure. The contents of the structure should match the contents of the original JSON. This is the program:</p>
  
  <pre><code>TYPES: BEGIN OF json_subobject,
           c TYPE i,
           d TYPE decfloat34,
         END OF json_subobject.
  TYPES: BEGIN OF json_object,
           a TYPE c LENGTH 10,
           b TYPE json_subobject,
         END OF json_object.
  
  DATA: foo TYPE json_object.
  
  DATA: lv_xmls TYPE string VALUE '{"a":"FooBar","b":{"c":9,"d":3.14}}',
        lv_xmlb TYPE xstring.
  
  TRY.
      lv_xmlb = cl_abap_codepage=&gt;convert_to(
                  source      = lv_xmls
                  codepage    = `UTF-8`
                  endian      = space
                  replacement = '#'
                  ignore_cerr = abap_false ).
    CATCH cx_parameter_invalid_range cx_sy_codepage_converter_init cx_sy_conversion_codepage cx_parameter_invalid_type.
      ENDTRY.
  
  WRITE: |Deserializing JSON ...|.
  NEW-LINE.
  
  CALL TRANSFORMATION id SOURCE XML lv_xmlb RESULT XML = foo.
  
  WRITE: '{'.
  NEW-LINE.
  WRITE: |  "a": "{ foo-a }",|.
  NEW-LINE.
  WRITE: |  "b": \{|.
  NEW-LINE.
  WRITE: |    "c": { foo-b-c },|.
  NEW-LINE.
  WRITE: |    "d": { foo-b-d },|.
  NEW-LINE.
  WRITE: |  \}|.
  NEW-LINE.
  WRITE: |\}|.
  </code></pre>
  
  <p>The output I would expect is:</p>
  
  <pre><code>Deserializing JSON ...
  {
    "a":"FooBar",
    "b":{
      "c":9,
      "d":3.14
    }
  }
  </code></pre>
  
  <p>But unfortunately, the output I get is:</p>
  
  <pre><code>Deserializing JSON ...
  {
    "a":"",
    "b":{
      "c":0,
      "d":0
    }
  }
  </code></pre>
  
  <p>It looks like the CALL TRANSFORMATION statement isn't doing anything to me.</p>
  
  <p>Is there an ABAP guru who could show me how to use CALL TRANSFORMATION in this (hopefully) simple case? I already looked around and found <a href="https://github.com/Keller-Michael/ABAP_and_JSON" rel="nofollow noreferrer">this helpful GitHub repo</a> demonstrating how to deserialize JSON to a class, but I'd prefer to deserialize JSON to a structure for simplicity. I'm not entirely sure how to use CALL TRANSFORMATION because I'm unfamiliar with XSLT and advanced XML features, so I would very much appreciate a ready-made solution if possible ...</p>
  
  <p>Thanks a lot in advance,</p>
  
  <p>Joshua</p>