Dynamic JSON elements and using them in a rule

in short: my use case is to have a rule, which can handle dynamic JSON elements.
a bit longer: I have a PTZ Webcam, which I have set up to presently send two pictures (eastbound and westbound). I have a python-script, which addresses the camera, has it to move to PresetEast, take a Picture, move to PresetWest and take another picture. The script then sends me a JSON with two paths to the two pictures.

If I’d like to have a third (or fourth) Preset and taken more pictures, I’d like to have the rule understand, that theres more pictures, than just two and use all of them. In my case, it sends an email with attachments.

presently the JSON looks like this:

{ "filename1":"/srv/openhab2-userdata/tmp/rossbuehelW-20180324-165324.jpg", "filename2":"/srv/openhab2-userdata/tmp/rossbuehelO-20180324-165324.jpg"}

the rule looks like this:

	// executing the python-script and waiting for the JSON return
	var String cmd = "python /srv/openhab2-userdata/webcam.py"
	var String bilderjson = executeCommandLine(cmd, 120000)

	// using static variable names for the two filenames
	var String filename1 = transform("JSONPATH", "$.filename1", bilderjson)	
	var String filename2 = transform("JSONPATH", "$.filename2", bilderjson)	

	// filling the List-Array with the paths	
	val List<String> attachmentUrlList = newArrayList(
		"file://" + filename1,
		"file://" + filename2)

I can easily change the JSON and of course the rule. I’m a bit blocked at the moment, I don’t know JSON so deeply. Thanks for your help!

You could loop through the JSON with i as a variable.
If the transform("JSONPATH", "$.filename" + i.toString, bilderjson) doesn’t work it will return an empty string.

Do you have control over the payload?

Instead of using

    filename1: "...",
    filename2: "..."

just use an array itslef, which is also a valid JSON string:

    "and more...",