Yet another JSON parser/serializer for ABAP. It works with release 7.02 or higher.
Since v1.2.0:
- there are changes in mapper interface, see "Mapping (field renaming)" section in the docs. In essence, implement
rename_node
method if needed,to_json
andto_abap
will be deprecated. As well ascreate_field_mapping
andcreate_camel_case
mappers - potentially
create_empty
static method may be deprecated. It is considered to usenew
instead (and/or direct creationcreate object
). Under consideration, post an issue if you have an opinion on this subject. - also
create_from
is potentially suboptimal, so preferclone
,filter
andmap
instead.
- parse into a flexible form, not fixed to any predefined data structure, allowing to modify the parsed data, selectively access its parts and slice subsections of it
- slicing can be particularly useful for REST header separation e.g.
{ "success": 1, "error": "", "payload": {...} }
where 1st level attrs are processed in one layer of your application and payload in another (and can differ from request to request)
- slicing can be particularly useful for REST header separation e.g.
- allows conversion to fixed abap structures/tables (
to_abap
) - convenient interface to manipulate the data -
set( value )
,set( structure )
,set( table )
,set( another_instance_of_ajson )
, also typed e.g.set_date
- also
setx
for text-based value setting likesetx( '/a/b:123' )
(useful e.g. for constants in APIs or in unit-tests)
- also
- seralization to string
- freezing (read only) instance content
- filtering. Create a JSON skipping empty values, predefined paths, or your custom filter.
- mapping - rule-based changing node names (e.g. snake case to camel case, upper/lower case)
- iterating through the array items or object members
- utility to calculate difference between 2 JSONs
Installed using abapGit, see also Installation section of the documentation.
data r type ref to zif_ajson.
data fragment type ref to zif_ajson.
r = zcl_ajson=>parse( '{"success": 1, "error": "", "payload": {"text": "hello"}}' ).
r->get( '/success' ). " returns "1"
r->get_integer( '/success' ). " returns 1 (number)
r->get_boolean( '/success' ). " returns "X" (abap_true - because not empty)
r->get( '/payload/text' ). " returns "hello"
r->members( '/' ). " returns table of "success", "error", "payload"
fragment = r->slice( '/payload' ).
fragment->get( '/text' ). " returns "hello" (the root has changed)
fragment->set(
iv_path = '/text'
iv_val = 'new text' ).
fragment->stringify( ). " {"text":"new text"}
See a lot more examples and usages in the Documentation.
- Forked from abaplint-abap-backend originally, at early stages
- The package is unit tested dynamically in Github actions thanks to the abap transpiler by @larshp
- Publication at SCN:
- Bicycles. #2 – AJSON – yet another abap json parser and serializer
- JSON alternative to maintenance views in ABAP
- Both above and future articles are in the Blog articles