Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Show all

Help Center/ Koo Command Line Interface/ FAQs/ Output Formats/ How Do I Define a JMESPath Expression?/ Which Built-in Functions Are Supported by JMESPath?

Which Built-in Functions Are Supported by JMESPath?

Updated on 2023-03-13 GMT+08:00

The built-in functions of JMESPath support the following data types:

  • number (integer and double-precision floating-point format in JSON)
  • string
  • boolean (true or false)
  • array (an ordered sequence of values)
  • object (an unordered collection of key-value pairs)
  • expression (denoted by &expression)
  • null

Built-in functions support different data types, as described in the following table. The special character @ in function arguments indicates that the current result is passed to the function as an input parameter.

Table 1 Built-in functions supported by JMESPath expressions

Built-in Function

Input Data Type

Output Data Type

Description

Example

abs

number

number

Returns the absolute value of the provided argument.

  • Expression: abs(1)

    Final result: 1

  • Expression: abs(-1)

    Final result: 1

avg

array[number]

number

Returns the average of the elements in the provided array.

Current result: [10, 15, 20]

Expression: avg(@)

Final result: 15

ceil

number

number

Returns the next highest integer value by rounding up if necessary.

Expression: ceil(`1.001`)

Final result: 2

contains

array|string, any

boolean

Returns true if the first given argument contains the second one, or otherwise returns false.

  • Expression: contains('foobar','foo')

    Final result: true

  • Current result: ["a", "b"]

    Expression: contains(@, 'a')

    Final result: true

ends_with

string, string

boolean

Returns true if the first character string ends with the second one, or otherwise returns false.

Current result: foobarbaz

Expression: ends_with(@,'baz')

Final result: true

floor

number

number

Returns the next lowest integer value by rounding down if necessary.

Expression: floor(`1.001`)

Final result: 1

join

string, array[string]

string

Returns all of the elements from the provided character string array joined using the given string argument as a separator.

Current result: ["a", "b"]

Expression: join(',', @)

Final result: "a, b"

keys

object

array

Returns an array containing the keys of the provided JSON object. Because JSON hashes are inherited and unordered, the keys associated with the provided object are also inherited and unordered. Implementations are not required to return keys in any specific order.

  • Current result: {"foo": "baz", "bar": "bam"}

    Expression: keys(@)

    The final result could be as follows:

    • ["foo", "bar"]
    • ["bar", "foo"]
  • Current result: {}

    Expression: keys(@)

    Final result: []

length

string|array|object

number

Returns the length of the given argument using the following type rules:

1. string: Returns the number of characters in the string.

2. array: Returns the number of elements in the array.

3. object: Returns the number of key-value pairs in the object.

  • Current result: current

    Expression: length(@)

    Final result: 7

  • Current result: ["a", "b", "c"]

    Expression: length(@)

    Final result: 3

  • Current result: {"foo": "bar", "baz": "bam"}

    Expression: length(@)

    Final result: 2

map

expression->any->any, array[any]

array[any]

Applies the expression in the input argument to every element in the array, and returns the array of results. An element of length N will produce a return array of length N.

Unlike a projection, map() will include the result of applying the expression for every element in the elements array, even if the result is null.

  • Current result: {"array": [{"foo": "a"}, {"foo": "b"}, {}, [], {"foo": "f"}]}

    Expression: map(&foo, array)

    Final result: ["a", "b", null, null, "f"]

  • Current result: [[1, 2, 3, [4]], [5, 6, 7, [8, 9] ]]

    Expression: map(&[], @)

    Final result: [[1, 2, 3, 4], [5, 6, 7, 8, 9]]

max

array[number]|array[string]

number

Returns the largest number in the provided array argument.

  • Current result: [10, 15]

    Expression: max(@)

    Final result: 15

  • Current result: ["abc", "drb"]

    Expression: max(@)

    Final result: drb

max_by

array, expression->number|expression->string

any

Returns the maximum element in an array by using the expression as a comparison key.

Current result: [{"name": "b", "age": 30, "age_str": "30"}, {"name": "a", "age": 50, "age_str": "50"}, {"name": "c", "age": 40, "age_str": "40"}]

For the preceding current result:

  • Expression: max_by(@, &age)

    Final result: {"age": 50, "age_str": "50", "name": "a"}

  • Expression: max_by(@, &age).age

    Final result: 50

  • Expression: max_by(@, &to_number(age_str))

    Final result: {"age": 50, "age_str": "50", "name": "a"}

merge

[object [, object ...]]

object

Accepts one or more objects as arguments, and returns a single object with subsequent objects merged. The key-value pairs of each subsequent object are added to the preceding object. This function is used to combine multiple objects into one. You can think of this as the first object being the base object, and each subsequent argument being the overrides that are applied to the base object.

  • Expression: merge(`{"a": "b"}`, `{"c": "d"}`)

    Final result: {"a": "b", "c": "d"}

  • Expression: merge(`{"a": "b"}`, `{"a": "override"}`)

    Final result: {"a": "override"}

  • Expression: merge(`{"a": "x", "b": "y"}`, `{"b": "override", "c": "z"}`)

    Final result: {"a": "x", "b": "override", "c": "z"}

min

array[number]|array[string]

number

Returns the smallest number in the provided array argument.

  • Current result: [10, 15]

    Expression: min(@)

    Final result: 10

  • Current result: ["a", "b"]

    Expression: min(@)

    Final result: "a"

min_by

array, expression->number|expression->string

any

Returns the smallest element in an array using the expression as a comparison key.

Current result: {"people": [{"name": "b", "age": 30, "age_str": "30"}, {"name": "a", "age": 50, "age_str": "50"}, {"name": "c", "age": 40, "age_str": "40"}]}

For the preceding current result:

  • Expression: min_by(people, &age)

    Final result: {"age": 30, "age_str": "30", "name": "b"}

  • Expression: min_by(people, &age).age

    Final result: 30

  • Expression: min_by(people, &to_number(age_str))

    Final result: {"age": 30, "age_str": "30", "name": "b"}

not_null

[any [, any ...]]

any

This function accepts one or more arguments, and evaluates them in order until a non-null argument is encountered. If the values of all arguments as resolved as null, KooCLI displays an error message and outputs the original JSON result.

  • Current result: {"a": null, "b": null, "c": [], "d": "foo"}

    Expression: not_null(no_exist, a, b, c, d)

    Final result: []

  • Current result: {"a": null, "b": null, "c": [], "d": "foo"}

    Expression: not_null(a, b, `null`, d, c)

    Final result: "foo"

  • Current result: {"a": null, "b": null, "c": [], "d": "foo"}

    Expression: not_null(a, b)

    Final result: null

reverse

string|array

string|array

Reverses the order of the input argument.

  • Current result: [0, 1, 2, 3, 4]

    Expression: reverse(@)

    Final result: [4, 3, 2, 1, 0]

  • Current result: []

    Expression: reverse(@)

    Final result: []

  • Current result: ["a", "b", "c"]

    Expression: reverse(@)

    Final result: ["c", "b", "a"]

  • Current result: "abcd"

    Expression: reverse(@)

    Final result: "dcba"

sort

array[number]|array[string]

array

Accepts an array argument and returns the sorted elements as an array.

The array must be a list of strings or numbers. Strings are sorted based on a dictionary.

  • Current result: ["b", "a", "c"]

    Expression: sort(@)

    Final result: ["a", "b", "c"]

  • Current result: [1, 4, 2]

    Expression: sort(@)

    Final result: [1, 2, 4]

sort_by

array, expression->number|expression->string

-

Sorts an array using an expression as the sort key. For each element in the array of elements, the expression is applied and the result value is used as the key for sorting the elements.

sort_by follows the same sorting logic as the sort function.

Current result: {"people": [{"name": "b", "age": 30, "age_str": "30"}, {"name": "a", "age": 50, "age_str": "50"}, {"name": "c", "age": 40, "age_str": "40"}]}

For the preceding current result:

  • Expression: sort_by(people, &age)[].age

    Final result: [30, 40, 50]

  • Expression: sort_by(people, &age)[0]

    Final result: {"age": 30, "age_str": "30", "name": "b"}

  • Expression: sort_by(people, &to_number(age_str))[1]

    Final result: {"age": 40, "age_str": "40", "name": c}

starts_with

string, string

boolean

Returns true if the first argument starts with the second one, or otherwise returns false.

  • Current result: foobarbaz

    Expression: starts_with(@, 'foo')

    Final result: true

  • Current result: foobarbaz

    Expression: starts_with(@,'baz')

    Final result: false

  • Current result: foobarbaz

    Expression: starts_with(@, 'f')

    Final result: true

sum

array[number]

number

Returns the sum of the provided array argument.

An empty array will produce a return value of 0.

  • Current result: [10, 15]

    Expression: sum(@)

    Final result: 25

  • Current result: []

    Expression: sum(@)

    Final result: 0

to_array

any

array

array: Returns the passed value.

number|string|object|boolean: Returns a one-element array containing the passed argument.

  • Expression: to_array(`[1, 2]`)

    Final result: [1, 2]

  • Expression: to_array('string')

    Final result: ["string"]

  • Expression: to_array(`0`)

    Final result: [0]

  • Expression: to_array(`true`)

    Final result: [true]

  • Expression: to_array(`{"foo": "bar"}`)

    Final result: [{"foo": "bar"}]

to_string

any

string

string: Returns the passed value.

number|array|object|boolean: The JSON encoded value of the object.

  • Expression: to_string(`2`)

    Final result: 2

  • Expression: to_string(`[]`)

    Final result: "[]"

  • Expression: to_string(false)

    Final result: "null"

to_number

any

number

string: Returns the parsed number.

number: Returns the passed value.

array|object|boolean|null: KooCLI displays an error message and outputs the original JSON result.

  • Expression: to_number(`2.3`)

    Final result: 2.3

  • Expression: to_number(`2`)

    Final result: 2

type

array|object|string|number|boolean|null

string

Returns the data type of the given argument as a string value.

The return value must be one of the following:

  • "number"
  • "string"
  • "boolean"
  • "array"
  • "object"
  • "null"
  • Expression: type('foo')

    Final result: "string"

  • Expression: type(`true`)

    Final result: "boolean"

  • Expression: type(`null`)

    Final result: "null"

  • Expression: type(`123`)

    Final result: number

  • Expression: type(`123.05`)

    Final result: number

  • Expression: type(`[1,2]`)

    Final result: "array"

  • Current result: {"abc": "123"}

    Expression: type(@)

    Final result: "object"

values

object

array

Returns an array of values of the provided JSON object. Because JSON hashes are inherited and unordered, the values associated with the provided object are also inherited and unordered. Implementations are not required to return values of the JSON object in any specific order.

Current result: {"a": "first", "b": "second", "c": "third"}

Expression: values(@)

The final result could be as follows:

  • ["first", "second", "third"]
  • ["first", "third", "second"]
  • ["second", "first", "third"]
  • ["second", "third", "first"]
  • ["third", "first", "second"]
  • ["third", "second", "first"]

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback