Search code examples
jsonjolt

Filter objects inside array


My JSON:

{
  "Id": 1124574,
  "Name": "3Test_pro_RTB (dec)20 06",
  "StartDate": "2024-12-09",
  "CreateTime": "2024-06-20T18:23:11Z",
  "Type": "CPM_BANNER_CAMPAIGN",
  "Status": "DRAFT",
  "State": "OFF",
  "SourceId": null,
  "Funds": {
    "Mode": "SHARED_ACCOUNT_FUNDS",
    "SharedAccountFunds": {
      "Refund": 0,
      "Spend": 0
    }
  },
  "RepresentedBy": {
    "Manager": null,
    "Agency": "Havas Digital"
  },
  "CpmBannerCampaign": {
    "BiddingStrategy": {
      "Search": {
        "BiddingStrategyType": "SERVING_OFF"
      },
      "Network": {
        "BiddingStrategyType": "CP_MAXIMUM_IMPRESSIONS",
        "CpMaximumImpressions": {
          "StartDate": "2024-12-09",
          "EndDate": "2024-12-15",
          "AutoContinue": "NO",
          "AverageCpm": 101000000,
          "SpendLimit": 3000000000
        }
      }
    },
    "Settings": [
      {
        "Option": "ADD_METRICA_TAG",
        "Value": "YES"
      },
      {
        "Option": "ADD_OPENSTAT_TAG",
        "Value": "NO"
      },
      {
        "Option": "ADD_TO_FAVORITES",
        "Value": "NO"
      },
      {
        "Option": "DAILY_BUDGET_ALLOWED",
        "Value": "YES"
      },
      {
        "Option": "ENABLE_AREA_OF_INTEREST_TARGETING",
        "Value": "NO"
      },
      {
        "Option": "ENABLE_SITE_MONITORING",
        "Value": "NO"
      },
      {
        "Option": "REQUIRE_SERVICING",
        "Value": "NO"
      },
      {
        "Option": "SHARED_ACCOUNT_ENABLED",
        "Value": "YES"
      }
    ],
    "VideoTarget": "CLICKS",
    "CounterIds": null,
    "FrequencyCap": {
      "Impressions": 3,
      "PeriodDays": 30
    }
  }
}

I need to modify it a little bit before next actions. I want to grab all from this json, but inside CpmBannerCampaign.Settings I need to filter array. Inside Settings we can see multiple objects. Object contains two properties: Option and Value. I should grab objects where Option equals one of this: ADD_OPENSTAT_TAG, ADD_METRICA_TAG, ADD_TO_FAVORITES, ENABLE_AREA_OF_INTEREST_TARGETING, ENABLE_CURRENT_AREA_TARGETING, ENABLE_REGULAR_AREA_TARGETING, ENABLE_SITE_MONITORING, REQUIRE_SERVICING. If not equals, than I should remove this object from array.

So for my JSON Settings should like this:

...//other part of JSON
"Settings": [
                        {
                            "Option": "ADD_METRICA_TAG",
                            "Value": "YES"
                        },
                        {
                            "Option": "ADD_OPENSTAT_TAG",
                            "Value": "NO"
                        },
                        {
                            "Option": "ADD_TO_FAVORITES",
                            "Value": "NO"
                        },
                        {
                            "Option": "ENABLE_AREA_OF_INTEREST_TARGETING",
                            "Value": "NO"
                        },
                        {
                            "Option": "ENABLE_SITE_MONITORING",
                            "Value": "NO"
                        },
                        {
                            "Option": "REQUIRE_SERVICING",
                            "Value": "NO"
                        }
                    ],
...

Is it possible to filer like this with JOLT?


Solution

  • You can form a pipe separated key in order to filter by Options after prefixing the right hand side of the leaf nodes of those objects by @1,Option such as

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&", //the elements other than "CpmBannerCampaign"
          "CpmBannerCampaign": {
            "*": "&1.&", //the elements other than "Settings"
            "Settings": {
              "*": {
                "*": "&3.&2.@1,Option.&"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "CpmBannerCampaign": {
            "*": "&1.&",
            "Settings": {
              "ADD_OPENSTAT_TAG|ADD_METRICA_TAG|ADD_TO_FAVORITES|ENABLE_AREA_OF_INTEREST_TARGETING|ENABLE_CURRENT_AREA_TARGETING|ENABLE_REGULAR_AREA_TARGETING|ENABLE_SITE_MONITORING|REQUIRE_SERVICING": {
                "*": "&3.&2[#2].&"
              }
            }
          }
        }
      }
    ]