Function: json.encode

Encodes given input object to JSON bytes, using UTF-8 charset.

If indent is provided, it must be either a boolean, or a dictionary with two optional string attributes: prefix (defaulting to an empty string), and indent (defaulting to four spaces). Setting indent to true is equivalent to setting it to an empty dictionary. Setting indent to false is equivalent to not setting it.

When the indentation is enabled, each JSON element begins on a new line beginning with prefix and followed by zero or more copies of indent according to the structural nesting.

Arguments

Arguments
data The input to be encoded.
indent Optional indentation options.

Returns

The resulting JSON bytes.

Raised exceptions

Exceptions
ValueError If the input contains types that cannot be JSON-encoded. Examples of such types include callable and bytes.

Examples

For more information, see Returning bytes.

# Encode message to JSON bytes, then encode bytes to Base64 text
- init:
    assign:
      - project: '${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}'
      - topic: TOPIC_ID
      - message:
          hello: world
      - base64Msg: '${base64.encode(json.encode(message))}'
- publish_message_to_topic:  # publishes message to Pub/Sub topic
    call: googleapis.pubsub.v1.projects.topics.publish
    args:
      topic: '${"projects/" + project + "/topics/" + topic}'
      body:
        messages:
          - data: '${base64Msg}'