Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to compare two strings in JOLT? #1274

Open
KarthikND01 opened this issue Jan 9, 2025 · 3 comments
Open

Is there a way to compare two strings in JOLT? #1274

KarthikND01 opened this issue Jan 9, 2025 · 3 comments

Comments

@KarthikND01
Copy link

I have below JSON, where I want to compare "categoryType" value with categories' object's "type" and pick up the matching object. Please find the details below,

Input

{
"categoryType": "A",
"categories": [
{
"type": "A",
"issue": "1"
},
{
"type": "B",
"issue": "2"
},
{
"type": "C",
"issue": "3"
}
]
}

Expected Output

{
"category" : {
"type" : "A",
"issue" : "1"
}
}

My JOLT Configuration

[
{
"operation": "shift",
"spec": {
"categories": {
"*": {
"type": {
"@(3,categoryType)": {
"@2": "category"
}
}
}
}
}
}
]

And the output I am getting is as below, which is incorrect because @(3,categoryType) value is "A", so it should only pick up the "type" matching the value "A". If I replace "@(3,categoryType)" with "A", it will pick up only one object matching the type "A". Any idea how to resolve this issue?
{
"category" : [ {
"type" : "A",
"issue" : "1"
}, {
"type" : "B",
"issue" : "2"
}, {
"type" : "C",
"issue" : "3"
} ]
}

@gbouget
Copy link

gbouget commented Jan 9, 2025

Something like this?

[
  {
    "operation": "shift",
    "spec": {
      "categoryType": "&",
      "categories": {
        "*": {
          "*": "@(1,type).&2.&1.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "categoryType": {
        "*": {
          "@(2,&)": ""
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "categories": {
        "*": {
          "@": "categories"
        }
      }
    }
  }
]

@barbaros74
Copy link

Your solution is pretty good @gbouget , yet might be shortened like this :

[
  {
    "operation": "shift",
    "spec": {
      "*Type": "&",
      "*ies": {
        "*": {
          "*": "@1,type.&(2,1)y.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*Type": {
        "*": {
          "@2,&": ""
        }
      }
    }
  }
]

@KarthikND01
Copy link
Author

Thank you @gbouget and @barbaros74 .. this solution is brilliant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants