# Can I call a python function inside the macro written in jinja?

**URL:** <https://discourse.getdbt.com/t/can-i-call-a-python-function-inside-the-macro-written-in-jinja/11989>\
**Category:** Help\
**Tags:** jinja, snowflake, python-models, macro, dbt-core\
**Created:** [February 10, 2024, 3:17am UTC](https://discourse.getdbt.com/t/can-i-call-a-python-function-inside-the-macro-written-in-jinja/11989 "2024-02-10T03:17:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pradeep244](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@pradeep244](https://discourse.getdbt.com/u/pradeep244)\
**Post date:** [February 10, 2024, 3:17am UTC](https://discourse.getdbt.com/t/can-i-call-a-python-function-inside-the-macro-written-in-jinja/11989/1 "2024-02-10T03:17:01Z")

</div>

- I have a column which contains a json string.

- using run\_query() , I captured the json string into a variable inside a macro written in jinja sql.

- I want to write a python function and call the function inside the macro to format the json string .

How can I call a python function inside the macro written in jinja ?

---

<div class="post-metadata">

**Author:** ![shivaramp](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@shivaramp](https://discourse.getdbt.com/u/shivaramp)\
**Post date:** [February 11, 2024, 9:50pm UTC](https://discourse.getdbt.com/t/can-i-call-a-python-function-inside-the-macro-written-in-jinja/11989/2 "2024-02-11T21:50:08Z")

</div>

@pradeep244 , you can create python macro similar to sql. please refer below link. [Python models | dbt Developer Hub](https://docs.getdbt.com/docs/build/python-models)

---

<div class="post-metadata">

**Author:** ![a\_slack\_user](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/a_slack_user/32/1629_2.png) [@a\_slack\_user](https://discourse.getdbt.com/u/a_slack_user)\
**Post date:** [February 12, 2024, 8:14am UTC](https://discourse.getdbt.com/t/can-i-call-a-python-function-inside-the-macro-written-in-jinja/11989/3 "2024-02-12T08:14:59Z")

</div>

dbt SQL models do not bring any data from the database onto the worker that’s running dbt. Trying to do this in a SQL model isn’t really the right pattern - and you should understand when exactly your query will be executed because it may be surprising.

Note: `@Mike Stanley` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1707725693242689?thread_ts=1707535045.694129&cid=CBSQTAPLG). It might not have transferred perfectly.

---

<div class="post-metadata">

**Author:** ![a\_slack\_user](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/a_slack_user/32/1629_2.png) [@a\_slack\_user](https://discourse.getdbt.com/u/a_slack_user)\
**Post date:** [February 12, 2024, 8:16am UTC](https://discourse.getdbt.com/t/can-i-call-a-python-function-inside-the-macro-written-in-jinja/11989/4 "2024-02-12T08:16:13Z")

</div>

The simplest solution is to create your transformation as a UDF on the database using whatever features it provides for that (many cloud data warehouses support UDFs written in Python, JS, etc)

Note: `@Mike Stanley` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1707725768262829?thread_ts=1707535045.694129&cid=CBSQTAPLG). It might not have transferred perfectly.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)\
**Post date:** [February 19, 2024, 8:17am UTC](https://discourse.getdbt.com/t/can-i-call-a-python-function-inside-the-macro-written-in-jinja/11989/5 "2024-02-19T08:17:10Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
