bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Need help with joltTransform spec #1214

Open vishalpatel-rf opened 11 months ago

vishalpatel-rf commented 11 months ago

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.