Search code examples
jsonjolt

JOLT config fix


I've a JSON like this:

{
  "result": {
    "Ads": [
      {
        "Id": 16198570913,
        "AdGroupId": 5453796748,
        "Status": "ACCEPTED",
        "CpmBannerAdBuilderAd": {
          "TurboPageId": 533,
          "TurboPageModeration": null,
          "Creative": {
            "CreativeId": 113152230434,
            "ThumbnailUrl": "/8383ee33-b38f-44b9-af79-1ea48835892d/orig",
            "PreviewUrl": "/1131522304/preview"
          },
          "ErirAdDescription": null,
          "AutogeneratedErirAdDescription": null
        }
      },
      {
        "Id": 16198570914,
        "CampaignId": 111544648,
        "AdGroupId": 5453796748,
        "Status": "ACCEPTED",
        "CpmVideoAdBuilderAd": {
          "Creative": {
            "CreativeId": 241131522305,
            "ThumbnailUrl": "/56360bcb-b65f-484c-8bdb-0e9e8006f739/orig",
            "PreviewUrl": "1131522305/preview"
          },
          "ErirAdDescription": null,
          "AutogeneratedErirAdDescription": null
        }
      }
    ]
  }
}

I want to go through result and grab Ads array with all elements but with certain keys. I need AdGroupId and also CpmBannerAdBuilderAd or CpmVideoAdBuilderAd with almost all things inside it: inside Creative i need only CreativeId. Other keys not inside Creative I should include to result. But keys that are nulls I should remove from result json. So I expect to get this result:

{
  "Ads": [
    {
      "AdGroupId": 5453796748,
      "CpmBannerAdBuilderAd": {
        "TurboPageId": 533,
        "Creative": {
          "CreativeId": 113152230434
        }
      }
    },
    {
      "AdGroupId": 5453796748,
      "CpmBannerAdBuilderAd": {
        "Creative": {
          "CreativeId": 241131522305
        }
      }
    }
  ]
}

My attempt was:

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "Ads": {
          "*": {
            "AdGroupId": "[&1].AdGroupId",
            "CpmBannerAdBuilderAd|CpmVideoAdBuilderAd": "[&1].CpmBannerAdBuilderAd" //wrong thing
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "Ads[]"
    }
  }
]

But I don't understand how to proper make this string "CpmBannerAdBuilderAd|CpmVideoAdBuilderAd": "[&1].CpmBannerAdBuilderAd" Because I hardcoded it to always be as CpmBannerAdBuilderAd, but it should be CpmBannerAdBuilderAd or CpmVideoAdBuilderAd depends on key value. How can I fix it? Also I need to fix Creative key to grab only CreativeId.

UPD

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "AdGroupId": "&2[&1].&",
            "Cpm*|Cpc*": {
              "*": "&3[&2].&",
              "Creative": {
                "CreativeId": "&4[&3].&2.&1.&"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]

Solution

  • You can use the following single shift transformation spec :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "AdGroupId": "&2[&1].&",
                "Cpm*": {
                  "TurboPageId": "&3[&2].&1.&",
                  "Creative": {
                    "CreativeId": "&4[&3].&2.&1.&"
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    where

    • * represents zero, one, or multiple characters, above Cpm* means all keys starting with Cpm as an example
    • & wildcard is a replicator for the keys from any level

    Edit : Rather you can prefer using the following transformation considering the case related to the unwanted null values :

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": {
                "Cpm*": {
                  "~*": "Null"//if these attibutes don't exist or are null, then make them literal "Null"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "AdGroupId": "&2[&1].&",
                "Cpm*": {
                  "*": {
                    "*": { "@1": "&5[&4].&3.&2" },
                    "Null": { "": "" }//Make the pairs to be disaapeared for those with "Null" literal values 
                  },
                  "Creative": {
                    "CreativeId": "&4[&3].&2.&1.&"
                  }
                }
              }
            }
          }
        }
      }
    ]