Skip to content

[Feature] Add indent to tojson and toyaml context methods #11210

Description

@mjsqu

Is this your first time submitting a feature request?

  • I have read the expectations for open source contributors
  • I have searched the existing issues, and I could not find an existing issue for this feature
  • I am requesting a straightforward extension of existing dbt functionality, rather than a Big Idea better suited to a discussion

Describe the feature

Add indent parameter to tojson and toyaml. These are straight mappings to the equivalent indent parameters in the respective json.dumps and yaml.safe_dump methods. Adding the indent parameter helps with debugging when developing with objects in Jinja.

Describe alternatives you've considered

No response

Who will this benefit?

This will help dbt macro developers to debug their processes.

Are you interested in contributing this feature?

Yes

Anything else?

This specifically came about because I wanted to make use of the sort_keys parameter, which is available in the tojson context method, but not the tojson Jinja filter:

{%- set myobj = {'a':{'ab':1},{'b':{'ba':2}}-%}

{# Not valid! #}
{{ myobj | tojson(sort_keys=False}}
{{ tojson(myobj,indent=2) }}

{# Valid #}
{{ tojson(myobj,sort_keys=False) }}
{{ myobj | tojson(indent=2}}

{# Valid after this change implemented #}
{{ tojson(myobj,sort_keys=False,indent=2) }}

Currently the last statement errors with:

Encountered an error: Error parsing inline query
Error while parsing node: inline_query BaseContext.tojson() got an unexpected keyword argument 'indent'

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting_responseenhancementNew feature or requestv1Issue was created against the V1 repo.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions