9

I got an anonymous array which I want to deserialize, here the example of the first array object

[
  { "time":"08:55:54",
    "date":"2016-05-27",
    "timestamp":1464332154807,
    "level":3,
    "message":"registerResourcePath ('', '/sap/bc/ui5_ui5/ui2/ushell/resources/')",
    "details":"","component":"sap.ui.ModuleSystem"},
  {"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":"URL prefixes set to:","details":"","component":"sap.ui.ModuleSystem"},
  {"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":"  (default) : /sap/bc/ui5_ui5/ui2/ushell/resources/","details":"","component":"sap.ui.ModuleSystem"}
]

I tried deserializing using CL_TREX_JSON_SERIALIZER, but it is corrupt and does not work with my JSON, here is why

Then I tried /UI2/CL_JSON, but it needs a "structure" that perfectly fits the object given by the JSON Object. "Structure" means in my case an internal table of objects with the attributes time, date, timestamp, level, messageanddetails. And there was the problem: it does not properly handle references and uses class description to describe the field assigned to the field-symbol. Since I can not have a list of objects but only a list of references to objects that solution also doesn't works.

As a third attempt I tried with the CALL TRANSFORMATION as described by Horst Keller, but with this method I was not able to read in an anonymous array, and here is why

My major points:

  • I do not want to change the JSON, since that is what I get from sap.ui.log
  • I prefere to use built-in functionality and not a thirdparty framework
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
inetphantom
  • 2,498
  • 4
  • 38
  • 61
  • Have you tried [this](https://scn.sap.com/community/abap/blog/2013/04/15/abap-2-json-and-json-2-abap-with-st)? – Jagger May 31 '16 at 08:34
  • @Jagger Yes it bases on the `Call transformation` which needs (by the syntax `CALL TRANSFORMATION demo_st_json_table SOURCE XML json RESULT` **`carriers = result.`**) a named root node (in this case `carriers`) I do not have with my annonymous array. – inetphantom May 31 '16 at 08:43
  • Would a change of the json "on the fly" be acceptable? I would read this json into a string (assuming it is always that small) and then glue `"anonymous" : {` at the beginning and `}` at the end. Then try to use `CALL TRANSFORMATION` for whatever you need and strip this glued parts in the output if needed at all. – Jagger May 31 '16 at 08:53
  • @Jagger it will get very long. It is the clientlog of an ui5 application (2000 of those obects?). And I would prefer to not use such a hack. It could be a solution. A [parser](http://help.sap.com/abapdocu_740/en/index.htm?file=abenjson_reader_glosry.htm) would be an other approach – inetphantom May 31 '16 at 09:06

2 Answers2

2

Your problem comes out not from the anonymity of array, but from the awkwardness of SAP JSON (De)serializer, which doesn't respect double quotes, which enclose JSON attributes. The issue is thoroughly described in this answer.
If you don't want to change your JSON on-the-fly, the only way you have is to change CL_TREX_JSON_DESERIALIZER class like this.

Community
  • 1
  • 1
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
2

/UI5/CL_JSON_PARSER parses JSONs with unknown format.

Note that it's got "for internal use" written on it so many times that you probably should take it seriously and clone its code to fixate it.

Florian
  • 4,821
  • 2
  • 19
  • 44