Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support for key-value objects in JSON #194

@mbtools

Description

@mbtools

I have the following JSON:

{
  "created": "2025-01-10T08:05:38.730Z",
  "modified": "2025-01-10T08:30:59.013Z",
  "1.0.0": "2025-01-10T08:05:38.747Z",
  "2.0.0": "2025-01-10T08:30:59.013Z"
}

Is there a direct way to get this to an ABAP structure?

A corresponding ABAP type would be as follows:

TYPES:
  BEGIN OF ty_key_val,
    key TYPE string,
    val TYPE string,
  END OF ty_key_val,
  ty_ajson_keys TYPE STANDARD TABLE OF ty_key_val WITH KEY key.

DATA(data) = VALUE ty_ajson_keys(
  ( key = 'created'  val = '2025-01-10T08:05:38.730Z' )
  ( key = 'modified' val = '2025-01-10T08:30:59.013Z' )
  ( key = '1.0.0'    val = '2025-01-10T08:05:38.747Z' )
  ( key = '2.0.0'    val = '2025-01-10T08:30:59.013Z' ) ).

DATA(json) = zcl_ajson=>new( )->keep_item_order( )->set(
  iv_path = '/'
  iv_val  = data ).

DATA(out) = json->stringify( 2 ).

However, this results in a JSON array:

[
  {
    "key": "created",
    "val": "2025-01-10T08:05:38.730Z"
  },
  {
    "key": "modified",
    "val": "2025-01-10T08:30:59.013Z"
  },
  {
    "key": "1.0.0",
    "val": "2025-01-10T08:05:38.747Z"
  },
  {
    "key": "2.0.0",
    "val": "2025-01-10T08:30:59.013Z"
  }
]          

The value part could also be an object or array, for example:

{
  "player1": { 
    "name": "Toni",
    "city": "Toronto"
  },
 ...
 "player275": {
    "name": "Toni",
    "city": "Toronto"
  }
}

Could we enhance the logic so it detects that there is a "key" field named "key" and converts the data to the JSON object listed at the top (not an array)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions