Quantcast
Channel: krypted – krypted
Viewing all articles
Browse latest Browse all 1241

Google Cloud Function to convert JSON to YAML

$
0
0

This function takes JSON data as an input, uses the json and yaml modules to convert JSON data to YAML and then returns with the YAML. Deploy the function, as a Python 3.7 runtime, pasting the following code into the code field:

import json
import yaml

def main(event, context):
  # Get the JSON data from the event
  json_data = event['data']

  # Convert the JSON data to YAML
  yaml_data = yaml.safe_dump(json_data, default_flow_style=False)

  # Return the YAML data
  return yaml_data
"""
Once deployed, invoke the function with a POST request to the function's URL. To invoke the function:

curl -X POST -H "Content-Type: application/json" -d '{
  "name": "Chuck U Farley",
  "age": 17
}' https://us-central1-my-project.cloudfunctions.net/json-to-yaml

The response would appear as follows:

name: Chuck U Farley
age: 17
"""

The post Google Cloud Function to convert JSON to YAML appeared first on krypted.


Viewing all articles
Browse latest Browse all 1241

Trending Articles