jsonjolt

Fix JOLT config to get missed value


Source JSON:

[
  {
    "updated": "2025-04-07T12:59:07+03:00",
    "author": {
      "name": "Adriver::RestAPI::Statistics"
    },
    "content": {
      "type": "text/xml",
      "statUniqueObject": {
        "object": {
          "id": 810069,
          "type": "AD"
        },
        "stat": {
          "item": [
            {
              "exp": 34725,
              "click": 789,
              "reach": 331399,
              "date": "2025-04-05"
            },
            {
              "exp": 32090,
              "click": 747,
              "reach": 344560,
              "date": "2025-04-06"
            }
          ]
        }
      }
    }
  }
]

Idea is - grab stat.item array an add to each element of array id from object. My JOLT config:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "content": {
          "statUniqueObject": {
            "object": {
              "id": "[&4].id"
            },
            "stat": {
              "item": {
                "*": {
                  "*": "[&1].&"
                }
              }
            }
          }
        }
      }
    }
  }
]

Result:

[
  {
    "id": 810069,
    "exp": 34725,
    "click": 789,
    "reach": 331399,
    "date": "2025-04-05"
  },
  {
     //no id here
    "exp": 32090,
    "click": 747,
    "reach": 344560,
    "date": "2025-04-06"
  }
]

I expect to get field id for all elements in item array, but it goes only for 1st element. How to fix it?


Solution

  • You can grab it within the objects of the array by picking after going 3 levels up such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "content": {
              "statUniqueObject": {
                "stat": {
                  "item": {
                    "*": {
                      "@3,object.id": "[&1].id",
                      "*": "[&1].&"
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]