Skip to Content
0
Mar 16, 2021 at 02:21 PM

Dynamic JSON to ABAP via XSLT?

214 Views Last edit Mar 16, 2021 at 02:30 PM 2 rev

I have a JSON string at hand out of I need to get some data. The JSON looks something like this:

{
	"version": "1.2.0",
	"_version": "1.2.0",
	"PETER": {...},
	"PAUL": {...},
	"PASCAL": {
		"foo": {
			"3WO90XZ1DX191BL21WR1SF9I1": {
				"fromGroup": "3WO90XZ1DX191BL21WR1SF36H",
				"toGroup": "3WO90XZ1DX191BL21WR1SFYS9"
			},
			"id-1615798772904-545": {
				"fromGroup": null,
				"toGroup": "3WO90XZ1DX191BL21WR1SF36H",
				"item": {
					"id": "id-1615798772904-545",
					"original": "ABCD:12_34"
				}
			},
			"id-1615798798727-556": {
				"fromGroup": null,
				"toGroup": "3WO90XZ1DX191BL21WR1SF36H",
				"item": {
					"id": "id-1615798798727-556",
					"original": "XYZ:47_11a"
				}
			},
			"id-1615537093114-316": {
				"fromGroup": null,
				"toGroup": "id-1615537093115-317",
				"item": {
					"id": "id-1615537093114-316",
					"original": "",
					"title": "Irrelevant Item",
					"subTitle": "03/12/2021, 09:16:53"
				}
			}
		},
		"bar": {...}
		"qux": {...},
		"_version": "1.0.0"
	},
	"MARY": {...}
}

I need the following data: For a Name that is given as parameter (e.g. "PASCAL", i need the item id and original of all subobjects of "foo", if the original exists and is not initial. So I expect something like this as output:

<object>
	<id>id-1615798772904-545</id>
	<original>ABCD:12_34</original>
</object>
<object>
	<id>id-1615798798727-556</id>
	<original>XYZ:47_11a</original>
</object>

The number of name nodes is dynamic, and it may even be that the name does not exist at all (in which case I'd expect either an empty output, or an exception I can catch). The existence and order of objects foo, bar and qux is also not granted.

I have found several threads concerning how to handle JSON in XSLT, how to handle dynamic names, how to filter depending on values of subobjects, but I cannot seem to piece it together for my life.

I have also considered using cl_sxml_string_reader, and it looks pretty neat until I get to to the "foo" node. after that, since I need to render the subobjects, I'd need to switch to IXML classes, and I hope that there is a more performant solution than that. However, if there isn't, I will use that version.

Thanks in advance

Ole