0
votes

am trying to transform this json input using jolt. please help whats wrong with my spec.

JSON Input

{
  "ResultSets": {
    "ClassPaths": {
      "currentRow": 0,
      "fields": [
        {
          "name": "path"
        },
        {
          "name": "loadOrder"
        }
      ],
      "rows": [
        [
          "/a/b/genenerator.jar",
          "-10"
        ],
        [
          "/a/b/server.jar",
          "0"
        ]
      ]
    },
    "DisabledComponents": {
      "currentRow": 0,
      "fields": [
        {
          "name": "name"
        },
        {
          "name": "location"
        }
      ],
      "rows": [
        [
          "ActiveDirectoryLdapComponent",
          "/a/b/component.hda"
        ],
        [
          "AppAdapterCore",
          "/a/b/AdapterCore.hda"
        ]
      ]
    }
  }
}

Expected JSON Output

{
  "ResultSets" : {
    "ClassPaths" : [ {
      "path" : "/a/b/genenerator.jar",
      "loadOrder" : "-10"
    }, {
      "path" : "/a/b/server.jar",
      "loadOrder" : "0"
    } ],
     "DisabledComponents" : [ {
      "name" : "ActiveDirectoryLdapComponent",
      "location" : "/a/b/component.hda"
    }, {
      "name" : "AppAdapterCore",
      "location" : "/a/b/AdapterCore.hda"
    } ]
  }
}

My Spec is

[
  {
    "operation": "shift",
    "spec": {
      "ResultSets": {
        "*": {
          "rows": {
            "*": {
              "*": "ResultSets.&1.@(3,fields[&].name)"
            }
          }
        }
      }
    }
  }
]

if i replace * with the key name(ClassPath or DisabledComponents) it works but gives only json with that key.

I cannot hardcode the key cos its dynamic and we dont know what comes there beforehand.

So i want a spec which works without specifying exact key (ClassPath or DisabledComponents) in the spec.

Please help.

Thanks, Hari

1

1 Answers

0
votes

You are almost correct. Here is the spec which produces the expected output:

[
  {
    "operation": "shift",
    "spec": {
      "ResultSets": {
        "*": {
          "rows": {
            "*": {
              "*": "ResultSets.&3[&1].@(3,fields[&].name)"
            }
          }
        }
      }
    }
  }
]

Explanation:

  • &1 - refers to the index of the object inside the array
  • Instead we want to place it under the same index but wrapped by the object which is three levels up. That's why instead of &1 we use &3[&1]