Search code examples
conditional-statementsjmespath

How to create a conditional node with JMESPath


With JMESPath based on the following:

  • If the input is

    { "app": { "usertype": "power" } }
    

    I would like to create

    { "output": { "aslist": true } }
    
  • If the input is

    { "app": { "usertype": "simple" } }
    

    I would like to create:

    { "output": { "aslist": false } }
    

I can create the output but not the conditional part. Seems like a simple if then else but I can't find any documentation on that.

Any suggestions?


Solution

  • You can simply use the evaluation of a condition in JMESPath as a value for your resulting JSON.

    Given the query:

    {output: {aslist: app.usertype == 'power'}}
    
    • On your example JSON:
      { 
        "app": { 
          "usertype": "power" 
        } 
      }
      
      This would give
      {
        "output": {
          "aslist": true
        }
      }
      
    • On your example JSON:
      { 
        "app": { 
          "usertype": "simple" 
        } 
      }
      
      This would give
      {
        "output": {
          "aslist": false
        }
      }
      

    But, of course, since it is a simple evaluation of a condition based on your simplified example it would also give you a false for anything that is not of usertype being power.