Hi Christoffer,
you can indeed iterate over a top map in Mustache in CFEngine. I
included that in the example below. That's very similar to the example I
posted for Robin just yesterday. Here I used the custom CFEngine
extensions to Mustache: `-top-` pointing to the top object, and `@`
pointing to the current iteration key.
I also included an example of using mapdata("json_pipe") calling `jq`
externally to convert a map to an array of arrays. It's easy to adapt
the example to whatever you need specifically, but maybe the Mustache
iteration is all you need.
HTH
Ted
#+begin_src cfengine3
bundle agent main
{
vars:
"map" data => '
{
"val1":{"name":"val1","key1":"val1_1","key2":"val1_2"},
"val2":{"name":"val2","key1":"val2_1","key2":"val2_2"}
}';
"output" string => string_mustache('
{{#-top-}}
key is {{{@}}}
name is {{{name}}}
{{/-top-}}
', map);
"map_as_array" data => mapdata("json_pipe", '$(def.jq) "to_entries|.[]|[.key, .value]"', map);
"map_as_array_str" string => format("%S", map_as_array);
reports:
"$(this.bundle): output = $(output)";
"$(this.bundle): map as array = $(map_as_array_str)";
}
#+end_src
#+begin_src text
% cf-agent -KI -f ./
test_mustache_iteration2.cf
R: main: output =
key is val1
name is val1
key is val2
name is val2
R: main: map as array = [["val1",{"key1":"val1_1","key2":"val1_2","name":"val1"}],["val2",{"key1":"val2_1","key2":"val2_2","name":"val2"}]]
#+end_src