Home > front end >  JOLT shift through properties with different names
JOLT shift through properties with different names

Time:05-20

I have a JSON:

{
  "relations": {
    "advertiser_id": {
      "9968": {
        "name": "Advance/Unicredit",
        "id": 9968
      },
      "10103": {
        "name": "Advance/ ORIMI",
        "id": 10103
      }
    },
    "campaign_id": {
      "256292": {
        "name": "Interests_Aidata",
        "id": 256292,
        "advertiser_id": 9968
      },
      "257717": {
        "name": "G_14.04",
        "id": 257717,
        "advertiser_id": 10103
      }
    }
  }
}

I thought that it's an easy shift operation, but I'm stuck because of all values inside random property names like "9968": I don't understand how to move through json with these different propertie names.

Expected Output:

[
  {
    "name": "Interests_Aidata",
    "id": 256292,
    "advertiser_id": 9968
  },
  {
    "name": "G_14.04",
    "id": 257717,
    "advertiser_id": 10103
  }
]

UPDATE

Is it possible to add top-level (under relations) advertiser_id or campaign_id as additional propety like in an example?

[
  {
    "name": "Interests_Aidata",
    "id": 256292,
    "advertiser_id": 9968,
    "entity_type": "campaign_id"
  },
  {
    "name": "G_14.04",
    "id": 257717,
    "advertiser_id": 10103,
    "entity_type": "campaign_id"
  }
]

CodePudding user response:

If your aim is to accumulate each array of objects under those properties(advertiser_id and campaign_id), then use the following spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "@": "&2"
          }
        }
      }
    }
  }
]

If you're interested in only the stuff under campaign_id as lately edited, then use

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "campaign_id": {
          "*": {
            "@": ""
          }
        }
      }
    }
  }
]

The following spec can be used the desired result determined by the last update

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "campaign_id": {
          "*": {
            "*": "[#2].&",
            "$1": "[#2].entity_type"
          }
        }
      }
    }
  }
]
  • Related