All Products
Search
Document Center

Resource Orchestration Service:Fn::Jq

Last Updated:Mar 15, 2024

The built-in function Fn::Jq function is used to support jq features and sorts values in a JSON string by using a filter.

Declaration

  • JSON

    {
      "Fn::Jq": [
        method,
        script,
        object
      ]
    }
  • YAML

    • Syntax for the full function name:

      Fn::Jq: method,script,object
    • Syntax for the short form:

      !Jq [method,script,object]

Parameters

  • method: Required. The parameter value must be of the String type.

    Valid values:

    • First: the first value that is sorted by the filter

    • All: all values that are sorted by the filter

  • script: Required. The parameter value must be of the String type. This parameter specifies the jq script that you want to use as a filter to sort values in a JSON string. For more information about jq features, see jq Manual (development version).

  • object: Required. This parameter specifies the JSON string.

Return value

The values in the string that are sorted by the filter.

Examples

Single filter

In the following example, the function returns the first value sorted by the .test filter. When you specify a JSON string, the value at the test key is returned. If the value does not exist, null is returned.

!Jq
  - First
  - .test
  - '{"test":"test"}'

Result:

test

Multiple filters

Note

Separate multiple filters with vertical bars (|).

In the following example, the value sorted by the .parameters[] filter is sorted by another filter. The filters are separated by a vertical bar (|). When you specify a JSON string, the values at the .parameters[] | {"param_name": .name, "param_type":.type} key are returned. If the values do not exist, null is returned. You must check the values whose Key is parameters based on the filter to obtain the initial return values. In the return values, you must check the values whose Key is name and type to obtain the final return values. If no values are sorted by the filters, null is returned.

!Jq
  - All
  - '.parameters[] | {"param_name": .name, "param_type":.type}'
  - changeSet:
      items: []
      kind: git
    id: 2013-12-27_00-09-37
    parameters:
      - name: PKG_TAG_NAME
        value: trunk
      - name: GIT_COMMIT
        value: master
      - name: TRIGGERED_JOB
        value: trunk-buildall

Result:

- param_name: PKG_TAG_NAME
- param_name: GIT_COMMIT
- param_name: TRIGGERED_JOB