Stay organized with collections
Save and categorize content based on your preferences.
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.
# 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 topiccall:googleapis.pubsub.v1.projects.topics.publishargs:topic:'${"projects/"+project+"/topics/"+topic}'body:messages:-data:'${base64Msg}'
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Function: json.encode\n\nEncodes given input object to JSON bytes, using UTF-8 charset.\n\nIf `indent` is provided, it must be either a boolean, or a dictionary\nwith two optional string attributes: `prefix` (defaulting to an empty string),\nand `indent` (defaulting to four spaces).\nSetting `indent` to `true` is equivalent to setting it to an empty dictionary.\nSetting `indent` to `false` is equivalent to not setting it.\n\nWhen the indentation is enabled, each JSON element begins on a new line\nbeginning with `prefix` and followed by zero or more copies of `indent`\naccording to the structural nesting.\n\nArguments\n---------\n\nReturns\n-------\n\nThe resulting JSON bytes.\n\nRaised exceptions\n-----------------\n\nExamples\n--------\n\nFor more information, see\n[Returning bytes](https://cloud.google.com/workflows/docs/reference/syntax/datatypes.md#bytes). \n\n```yaml\n# Encode message to JSON bytes, then encode bytes to Base64 text\n- init:\n assign:\n - project: '${sys.get_env(\"GOOGLE_CLOUD_PROJECT_ID\")}'\n - topic: TOPIC_ID\n - message:\n hello: world\n - base64Msg: '${base64.encode(json.encode(message))}'\n- publish_message_to_topic: # publishes message to Pub/Sub topic\n call: googleapis.pubsub.v1.projects.topics.publish\n args:\n topic: '${\"projects/\" + project + \"/topics/\" + topic}'\n body:\n messages:\n - data: '${base64Msg}'\n```"]]