Search code examples
jsonjolt

Jolt Spec skipping over first element in array


Input JSON :

[
  {
    "e": "id1",
    "c": [
      {
        "title": "title1",
        "enable": false
      }
    ]
  },
  {
    "e": "id2",
    "c": [
      {
        "title": "title2",
        "enable": true
      }
    ]
  }
]

want to create new property called filtered at same level as c that contains any item from c with enable property not set to false. So expected output is

[
  {
    "e": "id1",
    "c": [
      {
        "title": "title1",
        "enable": false
      }
    ],
   "filtered": []
  },
  {
    "e": "id2",
    "c": [
      {
        "title": "title2",
        "enable": true
      }
    ],
  "filtered": [
      {
        "title": "title2",
        "enable": true
      }
    ]
  }
]

I am using following spec

[
  {
    "operation": "shift",
    "spec": {
      "@": "&"
    }
    },
  {
    "operation": "modify-default-beta",
    "spec": {
      "root": {
        "*": {
          "c": {
            "*": {
              "enable": true
            }
          }
        }
      }
    }
    },
  {
    "operation": "shift",
    "spec": {
      "root": {
        "*": {
          "c": {
            "*": {
              "enable": {
                "true": {
                  "@2": "[&5].filtered[]"
                }
              }
            }
          },
          "@": ""
        }
      }
    }
  }
 ]

But it skips over the first element at top level completely and generate output like this

[
  {
    "e": "id1",
    "c": [
      {
        "title": "title1",
        "enable": true
      }
    ]
  },
  {
    "e": "id2",
    "c": [
      {
        "title": "title2",
        "enable": true
      }
    ],
    "filtered": [
      {
        "title": "title2",
        "enable": true
      }
    ]
  }
]

Will appreciate any help in getting the spec right.

for every valid input it seems like spec is skipping over the first element at top level completely


Solution

  • You can use the below jolt spec to get the expected output JSON

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "c": {
              "*": {
                "enable": {
                  "true": {
                    "@3": "[&5].filtered"
                  }
                }
              }
            },
            "@": "[]"
          }
        }
      },
      {
        "operation": "modify-default-beta",
        "spec": {
          "*": {
            "filtered": []
          }
        }
      }
    ]