Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

jmespath - Filter or conditional statement on a simple document?

On the below simple JSON document

{ "result" : 5 }

I would like to filter or apply a condition in order to get value matching result > 5

question from:https://stackoverflow.com/questions/65879118/filter-or-conditional-statement-on-a-simple-document

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JMESPath is not really meant to act on a single object.

In order to achieve this, though, you can convert the object to an array, with the function to_array and back to an object, using | [0], in order to extract back the first element of the list, as explained in working with nested data.

All together, this gives the query:

to_array(@)[?result>`5`] | [0]
  • Which, on the JSON:
    { "result" : 5 }
    
    gives:
    null
    
  • And, on the JSON:
    { "result" : 6 }
    
    gives:
    {
      "result": 6
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...