Tutorial: Build a function using a generic job

This tutorial provides step-by-step instructions to use a generic job to develop a function that converts YAML documents into JSON format.

Prerequisites

Before continuing with this tutorial, ensure you have completed the following prerequisites:

Create a function for conversion

  1. Open the LEARN_DEV project in Snoweaver with the LEARN_DEV_DEVELOPER role.

  2. Open the Functions page.

  3. Create the following function.

    Name:

    yaml_to_json
    

    Definition:

    def yaml_to_json(input: str):
       from ruamel.yaml import YAML
       import json
       yaml = YAML(typ='safe')
       return json.dumps(yaml.load(input))
    

    Validator - Jinja Template:

    {{ yaml_to_json('''
    ---
    doe: "a deer, a female deer"
    ray: "a drop of golden sun"
    pi: 3.14159
    xmas: true
    ''') }}
    

    Note

    You do not need to specify the ruamel.yaml package, as it is included among the pre-loaded Packages.

  4. Test the function.

    ../_images/214.png
  5. Save the function.

Create a generic job

  1. Open the Jobs page

  2. Apply the following configuration:

    Name:           yaml_to_json
    Job Type:       Generic
    Instance Type:  Function
    Functions:      yaml_to_json
    Output Format:  Json
    ../_images/312.png
  3. Specify the following job vairable:

    Name

    Type

    Test Value

    yaml_doc

    Text

    a:
    b: 2
    
    ../_images/413.png
  4. Use the following Jinja template for specifying the job Output:

    {{ yaml_to_json(_vars.yaml_doc) }}
    
  5. Test the template to validate the output.

    ../_images/513.png
  6. Click Make a Test Call to test the job. The output should match the results produced in the preceding step.

  7. Save the job.

  8. Click Build to build a job instance.

Test the function in a SQL statement

  1. Open a Snowflake worksheet and execute the following statements to test the function.

    USE ROLE LEARN_DEV_DEVELOPER;
    SELECT SNOWEAVER.LEARN_DEV.YAML_TO_JSON('
    pi: 3.14159
    xmas: true
    partridges:
       count: 1
       location: "a pear tree"
    ');
    
    ../_images/66.png