Raymond Camden
Raymond Camden

Reputation: 10857

How do I work with a list of simple, not-complex, values?

When looking at the docs for Lists (https://opensource.adobe.com/pdftools-sdk-docs/docgen/latest/templatetags.html#lists), it demonstrates an array of objects:

{
  "products": [
    {
      "productName": "Adobe Photoshop"
    },
    {
      "productName": "Adobe Premiere Pro"
    },
    {
      "productName": "Adobe InDesign"
    }
}

And in the Word doc, it uses:

{% repeating-section products %}
{{ productName }}
{% end-section %}

While this works, typically an array of names would not be objects, but just names:

{
 "products": [ "foo", "moo", "goo"]
}

How would I iterate over this in my Word document?

Upvotes: 1

Views: 35

Answers (1)

Raymond Camden
Raymond Camden

Reputation: 10857

As you pointed out, the docs assume an array of objects, not simple values, which is why when you are inside the repeating section, you can reference the key and the implication is that it will point to the current array item as it loops.

I took a look at the JSONata docs related to arrays (http://docs.jsonata.org/simple) and noticed the use of $[0]. The docs mentioned that $ refers to the entire input document. On a whim, I tried that in my Word doc:

{% repeating-section products %}
{{ $[0] }}
{% end-section %}

And of course changed my data to be simple values - and this worked. My guess is that inside the loop, the $[0] refers to the current array item. I also tested {{ $[0].name }} in my Word doc where name was a simple value, and it worked the same as {{ name }}, so it feels like this is a safe answer/suggestion.

Upvotes: 1

Related Questions