dbt-labs / dbt-labs/dbt

[Feature] Add indent to tojson and toyaml context methods

Open
#11,210 2 comments 0 reactions 0 assignees View on GitHub
awaiting_response engine:v1 stale type:feature
Dominant language
Rust
Stars
13.8k
Forks
2.6k
Avg merge
21h 31m
Merged PRs (30d)
56

Description

### Is this your first time submitting a feature request?

- [X] I have read the [expectations for open source contributors](https://docs.getdbt.com/docs/contributing/oss-expectations)
- [X] I have searched the existing issues, and I could not find an existing issue for this feature
- [X] 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:

```sql
{%- 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'

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.