Home > OS >  Jolt to Transform JSON Array and change labels
Jolt to Transform JSON Array and change labels

Time:06-22

I have a JSON as below.

[
  {
    "CATENTRY_ID": "3074457345616677728",
    "USAGE": "1",
    "LANGUAGE_ID": "-1",
    "STOREENT_ID": "10051",
    "ATTRTYPE_ID": "STRING",
    "ATTR_ID": "7741124012283333360",
    "ATTR_SEQUENCE": "0"
  },
  {
    "CATENTRY_ID": "3074457345616677729",
    "USAGE": "1",
    "LANGUAGE_ID": "-1",
    "STOREENT_ID": "10051",
    "ATTRTYPE_ID": "STRING",
    "ATTR_ID": "7741124012283333360",
    "ATTR_SEQUENCE": "0"
  }
]

Using JOLT operation I want to convert as below. Rename CATENTRY_ID to ProductId and remove USAGE from JSON

[
  {
    "ProductId": "3074457345616677728",
    "LANGUAGE_ID": "-1",
    "STOREENT_ID": "10051",
    "ATTRTYPE_ID": "STRING",
    "ATTR_ID": "7741124012283333360",
    "ATTR_SEQUENCE": "0",
  },
  {
    "ProductId": "3074457345616677729",
    "LANGUAGE_ID": "-1",
    "STOREENT_ID": "10051",
    "ATTRTYPE_ID": "STRING",
    "ATTR_ID": "7741124012283333360",
    "ATTR_SEQUENCE": "0",
  }
]

I have used below operation to achieve same, However rename is not working in my case.strong text

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&0",
        "LANGUAGE_ID": "LANGUAGE_ID"
      }
    }
  }
]

CodePudding user response:

This Spec will give you the solution you want you require 2 operations to do it :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[#2].&",
        "CATENTRY_ID": "[#2].ProductId"
      }
    }
    }, {
    "operation": "remove",
    "spec": {
      "*": {
        "USAGE": ""
      }
    }
    }

]

CodePudding user response:

You're so close, just filter for the attributes with trailing _ID, and leading ATTR_ in order to exempt the attribute with key name USAGE within the current shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*_ID|ATTR_*": "[&1].&",
        "CATENTRY_ID": "[&1].ProductId"
      }
    }
  }
]

the demo on the site enter image description here

  • Related