takotsubo
takotsubo

Reputation: 746

Remove nulls from jolt

I've a JSON:

[
  {
    "Type": "MOBILE_ADJUSTMENT",
    "MobileAdjustment": {
      "BidModifier": 110,
      "OperatingSystemType": "IOS"
    },
    "DesktopAdjustment": null,
    "SmartTvAdjustment": null
  }
]

I want to remove all nulls from it and also stay only Adjustment field. I tried with:

[
  {
    "operation": "default",
    "spec": {
      "*": {
        "*": "NULLS"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "NULLS": null,
          "*": {
            "@1": "&2"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "AdGroupId": 1
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*Adjustment": "&",
      "AdGroupId": "AdGroupId"
    }
  }
]

But it returns invalid result:

{
  "AdGroupId": 1,
  "MobileAdjustment": [
    {
      "BidModifier": 110,
      "OperatingSystemType": "IOS"
    },
    {
      "BidModifier": 110,
      "OperatingSystemType": "IOS"
    }
  ]
}

Somehow it builds array with two duplicates. How to fix that? I expect:

{
  "AdGroupId": 1,
  "MobileAdjustment": [
    {
      "BidModifier": 110,
      "OperatingSystemType": "IOS"
    }
  ]
}

Upvotes: 2

Views: 35

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65323

You can use the following transformation specs :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "#1": "AdGroupId",//hardcode the desired attribute by using # wildcard
        "Type": { "": "" },//remove this attribute by empty matching
        "*": {
          "*": "&1.&"//remove null-valued attributes while keeping the rest
        }
      }
    }
  },
  {//converts "1" to non-quoted 1
    "operation": "modify-overwrite-beta",
    "spec": {
      "AdGroupId": "=toInteger"
    }
  }
]

the demo on the site https://jolt-demo.appspot.com/ is :

enter image description here

Upvotes: 2

Related Questions