On a dashboard, I want to display Breakfast: Toast | Lunch: Pasta.
Now my question is which transformation I have to use within the HTTP binding for that? Using JSONPATH, this would be the necessary single transformation steps:
JSONPATH:$.mealType.name
JSONPATH:$.recipe.name
But how do I concat them? And how can the transformation handle a dynamic number of array elements (e. g. Breakfast, Lunch and Dinner)?
For the sake of completeness: I’m controlling the API, so in theory I could do the string dance on the server-side - but I’d appreciate it to not change the API if it can be accomplished in openHAB.
Even though the input is JSON, if you really want one item with formatted values you should consider REGEX transform instead.
But, I personally would also just populate 3 different items and achieve the desired formatting that you want by placing the the items into one custom widget.
But, I personally would also just populate 3 different items and achieve the desired formatting that you want by placing the the items into one custom widget.
Sorry, but I don’t understand how this would solve the problem challenge of having not always three but a different number of meals
You could put that into a transformation file if you prefer it instead of inline - makes it easier if you need this transformation in multiple items / things.
Your widget would be where this filtering takes place. The way the JSONpath transform works, if it doesn’t find a result it just returns the whole JSON string. So, if you have all three meals in the original object each meal item would have an appropriate string value. If one of the meals was missing from the original data then the missing meal, instead of a single value would be the whole JSON string. This would be easy to test for in a widget (e.g., presence of "mealTypeID" in the Item state) to determine whether to show the information about that meal or not.
Widgets themselves can also work with JSON strings. If you wanted to, you could just skip the JSONpath or REGEX or Script transform and store the whole returned JSON string in a single string item. Then you could do all the parsing in the widget expressions.