# Use JSON from a file in a template

**URL:** <https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814>\
**Category:** CMS Development\
**Tags:** templates, website-pages\
**Created:** [October 22, 2021, 3:32pm UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814 "2021-10-22T15:32:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![APino](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@APino](https://community.hubspot.com/u/APino)\
**Post date:** [October 22, 2021, 3:32pm UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/1 "2021-10-22T15:32:23Z")

</div>

Hello!

I have a custom template that uses a bit of json to generate some content, and I’m looking on the best way to update it without having to manually update the json in the template each time.

Nowadays I have something like this:

```lia-code-sample
var myjson = {"myJson": "info"};
//do things with json...

```

but I want to have something like this instead:

```lia-code-sample
var myJson = {{HubL.fetchmyfile.content}}
//do things with json...

```

I have tried to access files from module fields or storing the json in the template directory, but I don’t know how to use the info stored in those files.

Any suggestion?

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [October 25, 2021, 3:55pm UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/2 "2021-10-25T15:55:21Z")

</div>

@APino

You can create a file in the DM (like you would for a template partial) and copy your json there. Then you can reference that file with the [import tag](https://developers.hubspot.com/docs/cms/hubl/variables-macros-syntax#import)  
example:

```lia-code-sample
{% import '/Custom/partial.html' as try %}
<div>
 {{ try.test.fullName }}
</div>

```

In the partial, I have a variable set as **test**. That variable contains the json object.

In the import line, I have set the partial to **try.**

From the you can call you json

---

<div class="post-metadata">

**Author:** ![APino](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@APino](https://community.hubspot.com/u/APino)\
**Post date:** [October 25, 2021, 6:28pm UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/3 "2021-10-25T18:28:14Z")

</div>

Hi Dennis,

Thanks for answering. The main idea is that I want to avoid having to manually copy the json and add it to any file, because that’s what I already have working with the first solution, and instead I want to add the file to the CMS somewhere, then read the content so I can use it inside the template.  
Your solution will be slightly better if I need to reuse the structure, but that’s not the case.

To give context, the json is an OpenAPI definition, and I want to show the documentation. Here the current page (view source and scroll to the bottom to see the json in use):

> **[Waytobill - Swagger UI](https://www.waytobill.com/waytobill-swagger-ui)**
>
> Swagger sample for Waytobill's server checkout. Use the API special-key to test he authorization filters.

As any API, it evolves over time, and the idea is to avoid to copying a 20kb text file on my template every time it gets updated.

Thanks!

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [October 25, 2021, 6:44pm UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/4 "2021-10-25T18:44:27Z")

</div>

@APino ,

Makes total sense. Unfortunately, I do not believe it is doable at this time.

There are a few people who like to prove me wrong so will add them here to do so 😀

@piersg , @jpsanchez go ahead. Say I am wrong

---

<div class="post-metadata">

**Author:** ![jpsanchez](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jpsanchez/32/106486_2.png) [@jpsanchez](https://community.hubspot.com/u/jpsanchez)\
**Post date:** [October 26, 2021, 4:31am UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/5 "2021-10-26T04:31:19Z")

</div>

Hi, @APino , If i get it right this is an API services, Where is the service living? ( nodejs, endpoints, etc..)

There is a word in spanish i will translate it " IF Mahoma don´t go to the Mountain, The Mountain goes to Mahoma" … ( With Entreprise you can have a NodeJs in HubSpot server)

Waiting for your answer,

Best! 😉

JP

---

<div class="post-metadata">

**Author:** ![APino](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@APino](https://community.hubspot.com/u/APino)\
**Post date:** [October 26, 2021, 7:23am UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/6 "2021-10-26T07:23:20Z")

</div>

The service is living in its own webserver, but I get where you are going!

I now know that I can’t host the file in HubSpot CMS, so I can just host it from somewhere else and query it directly from the page with Javascript for usage.

That will probably work 🙂  
Thanks everyone!

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [October 26, 2021, 8:46am UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/7 "2021-10-26T08:46:18Z")

</div>

@APino you can host JSON files in Hubspot CMS and use it on a page, you just need a bit of JS to do it. That would probably be the easiest way around this, as you can have all your templates reference one file, and when you need to update it just replace the file. You can use fetch to get JSON:

```lia-code-sample
fetch('[link to your JSON file in Hubspot]').then(response => {
 return response.json();
}).then(data => {
 // Do things with JSON data here
 console.log(data);
}).catch(err => {
 // Do something for an error here
});

```

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [October 26, 2021, 11:25am UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/8 "2021-10-26T11:25:23Z")

</div>

You cheated with JavaScript 😜

---

<div class="post-metadata">

**Author:** ![APino](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@APino](https://community.hubspot.com/u/APino)\
**Post date:** [October 26, 2021, 12:21pm UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/9 "2021-10-26T12:21:06Z")

</div>

I ended up cheating as you say @dennisedson 😄  
I tried to host it on the HubSpot cms but for some reason the file didn’t load. (Probably some CORS 🙈)

Then I hosted the json in my cdn and loaded ok with Javascript just fine.  
Since I can control the file version on the cdn I don’t need to update the template anymore when updating the specifications.

Thanks everyone!

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [October 29, 2021, 9:59am UTC](https://community.hubspot.com/t/use-json-from-a-file-in-a-template/45814/10 "2021-10-29T09:59:37Z")

</div>

@dennisedson is it cheating if it’s the only simple answer? 🤔 😉
