Skip to content

Nested loop #314

@hpitzer69

Description

@hpitzer69

Hi, I am trying to loop an array inside an array in my template.json file for the desired output.

I created a simple payload to demonstrate:

Input file

{
  "root": {
    "type": "produce",
    "vegetables": [
      {
        "name": "tomato",
        "color": "red"
      },
      {
        "name": "cucumber",
        "color": "green"
      },
      {
        "name": "bell-pepper",
        "color": "yellow"
      }
    ],
    "fruit": [
      {
        "name": "banana",
        "color": "yellow"
      },
      {
        "name": "orange",
        "color": "orange"
      },
      {
        "name": "apple",
        "color": "green"
      }
    ]
  }
}

Working template - loop array's used separately

{
  "payload": {
    "produce-color": {
      "green": {
        "#loop($..fruit[?(@.color == 'green')])": {
          "name": "#currentvalueatpath($.name)"
        },
        "#loop($..vegetables[?(@.color == 'green')])": {
          "name": "#currentvalueatpath($.name)"
        }
      },
      "orange": {
        "#loop($..fruit[?(@.color == 'orange')])": {
          "name": "#currentvalueatpath($.name)"
        },
        "#loop($..vegetables[?(@.color == 'orange')])": {
          "name": "#currentvalueatpath($.name)"
        }
      },
      "red": {
        "#loop($..fruit[?(@.color == 'red')])": {
          "name": "#currentvalueatpath($.name)"
        },
        "#loop($..vegetables[?(@.color == 'red')])": {
          "name": "#currentvalueatpath($.name)"
        }
      },
      "yellow": {
        "#loop($..fruit[?(@.color == 'yellow')])": {
          "name": "#currentvalueatpath($.name)"
        },
        "#loop($..vegetables[?(@.color == 'yellow')])": {
          "name": "#currentvalueatpath($.name)"
        }
      }
    }
  }
}

Output for working template

{
    "payload": {
        "produce-color": {
            "green": [
                {
                    "name": "apple"
                },
                {
                    "name": "cucumber"
                }
            ],
            "orange": [
                {
                    "name": "orange"
                }
            ],
            "red": [
                {
                    "name": "tomato"
                }
            ],
            "yellow": [
                {
                    "name": "banana"
                },
                {
                    "name": "bell-pepper"
                }
            ]
        }
    }
}

Failing template - nested arrays - (color green only)

{
  "payload": {
    "produce-color": {
      "green": {
        "#loop($..fruit[?(@.color == 'green')])": {
          "name": "#currentvalueatpath($.name)",
          "other": {
            "#loop($.vegetables[?(@.color == 'green')])": {
              "name": "#currentvalueatpath($.name)"
            }
          }
        }    
      },
      "orange": {
        "#loop($..fruit[?(@.color == 'orange')])": {
          "name": "#currentvalueatpath($.name)"
        },
        "#loop($..vegetables[?(@.color == 'orange')])": {
          "name": "#currentvalueatpath($.name)"
        }
      },
      "red": {
        "#loop($..fruit[?(@.color == 'red')])": {
          "name": "#currentvalueatpath($.name)"
        },
        "#loop($..vegetables[?(@.color == 'red')])": {
          "name": "#currentvalueatpath($.name)"
        }
      },
      "yellow": {
        "#loop($..fruit[?(@.color == 'yellow')])": {
          "name": "#currentvalueatpath($.name)"
        },
        "#loop($..vegetables[?(@.color == 'yellow')])": {
          "name": "#currentvalueatpath($.name)"
        }
      }
    }
  }
}

Output for failing template - for color green

 "green": [
                {
                    "name": "apple",
                    "other": null
                }
            ],

Is there an option to get this to work with the current framework, or is this a limitation?

If there is an option, will that option work for more than two arrays nested?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions