samrathal
samrathal

Reputation: 77

How can we extract key value from JSON array literal in Jolt Spec/ Transformation

I have a use case, where we're getting JSON data in complicated fashion, i have translate this object as JSON array and now am unable to extract the key/value from the array, and also there is no guarantee that every time we'll receive the Tags.

Please suggest how we can extract this from array.

Input JSON :

[
  {
    "SourceId": "/Apple/bb842437dd4/sourceGroups/ALPHABAT/providers/Mobile.com/phone/isp",
    "Tags": "Name\": \"OMapplication\",\"Owner\": \"Breily",
    "Tagscopy": [
      "Name\": \"OMapplication\"",
      "\"Owner\": \"Breily"
    ],
    "ResourceName": "omapps"
  },
  {
    "SourceId": "/Apple/bb842437dd4/sourceGroups/ALPHABAT/providers/Mobile.com/phone/isp",
    "Tags": "mobile-source-usage\": \"apple-cloud",
    "Tagscopy": [
      "mobile-source-usage\": \"apple-cloud"
    ],
    "ResourceName": "omapps"
  }
]

Need to do operation on "Tagscopy", we're getting random data in this.

Desired JSON :

[
  {
    "SourceId": "/Apple/bb842437dd4/sourceGroups/ALPHABAT/providers/Mobile.com/phone/isp",
    "Tags": "Name\": \"OMapplication\",\"Owner\": \"Breily",
    "Name": "OMapplication",
    "Owner": "Breily",
    "ResourceName": "omapps"
  },
  {
    "SourceId": "/Apple/bb842437dd4/sourceGroups/ALPHABAT/providers/Mobile.com/phone/isp",
    "Tags": "mobile-source-usage\": \"apple-cloud",
    "mobile-source-usage": "apple-cloud",
    "ResourceName": "omapps"
  }
]

Jolt Spec Used :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "SourceIdcopy": "=split('/', @(1,SourceId))",
        "Tagscopy": "=split(',', @(1,Tags))",
        "SourceName": "=lastElement(@(1,SourceIdcopy))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "SourceIdcopy": ""
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "*": "&",
        "Tagscopy": {
          "*": "&"
        }
      }
    }
  }
]

Upvotes: 1

Views: 151

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65105

You can use the following transformation spec

[
  {// Split members of the "Tagscopy" array with integer(0,1) suffixed keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "Tagscopy": {
          "*": {
            "@": "&3.&2&1"
          }
        }
      }
    }
  },
  {// Split related strings by colon characters
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "Tagscopy*": "=split(': ', @(1,&))"
      }
    }
  },
  {// Match components of those array component1 against component2
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&1.&",
        "Tagscopy*": {
          "@1,&[1]": "&2.&1.@(2,&[0])"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "Tagscopy*": {
          "*": "=split('\"', @(1,&))"
        }
      }
    }
  },
  {// Prune undesired values for right-hand-side
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "Tagscopy*": {
          "*": "=join('', @(1,&))"
        }
      }
    }
  },
  {// Prune undesired values for left-hand-side(keys)
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "Tagscopy*": {
          "\"*\"": "[&2].&(0,1)",
          "*\"": "[&2].&(0,1)"
        }
      }
    }
  }
]

Upvotes: 1

Related Questions