CMS Development

TDwebdev
Contributeur | Partenaire solutions Diamond
Contributeur | Partenaire solutions Diamond

How to get a JSON from a hubdb column and use in hubl

Résolue

Hello,

I want to use the inserted JSON from a spefic column in hubdb.

I have two values in the JSON: colorname and colorurl

 

I want to get the JSON from  a specifc hubdb column and use it in a module via a foreach.

 

Example JSON file:

[{"textvalue":"Thorngrijs 11-05 Newtone", "urlvalue":"thorngrijs-11-05-newtone"},{"textvalue":"Rood 01-76 Newtone", "urlvalue":"rood-01-76-newtone"}]

 

In the above example you see two colors (Thorngrijs 11-05 Newtone and Rood 01-76 Newtone). They have both a textvalue and urlvalue. I want to insert that JSON in a column in hubdb and load the JSON via hubl in my module.

 

Example Hubl code:

 

{# Example get JSON from hubdb column #}

{% set objects = module.row.columns['kleur_decibel_red_structuur_mko'] %}

 

{# Example test array (this works) #}
{% set objects2 = [{"textvalue":"Thorngrijs 11-05 Newtone", "urlvalue":"thorngrijs-11-05-newtone"},{"textvalue":"Rood 01-76 Newtone", "urlvalue":"rood-01-76-newtone"}] %}

 

{# display output from hubdb ( i get the JSON string, but the foreach values are empty)

{{objects}}<br><br>

 

{#display  output from test hardcoded array (this works, but not the method what i want) #}
{{objects2}}<br><br>

 

{# Load  textvalue and urlvalue from the hubdb column (where the JSON text is inserted) #}
{% for obj in objects %}

Title: {{obj.textvalue}} <br>Slug: {{obj.urlvalue}}<br><br>

{% endfor %}

 

in objects2 you can see the JSON that i want to use with the foreach, this works. But i want to use the exact same JSON text only from the hubdb column. Via hubdb it doesnt work for some reason. I get no Title or Slug as output.



Vet Digital

Did my post solve your question? Help the community by marking it as a solution
0 Votes
1 Solution acceptée
miljkovicmisa
Solution
Contributeur de premier rang | Partenaire solutions Platinum
Contributeur de premier rang | Partenaire solutions Platinum

How to get a JSON from a hubdb column and use in hubl

Résolue

Okay, so now I got it, since json is in the field it is parsed as json string when you assign it to a variable, thus you need to use the fromjson filter in order to reconvert it to an object when read from the hubdb. More info on that filter here.

 

So your code should look something like this (I added my comment in all caps, you basically needed to convert json to object because hubl is not parsing json but hubl objects):

 

 

 

 

{# Example test array (this works) #}
{% set objects2 = [{"textvalue":"Thorngrijs 11-05 Newtone", "urlvalue":"thorngrijs-11-05-newtone"},{"textvalue":"Rood 01-76 Newtone", "urlvalue":"rood-01-76-newtone"}] %}

 

{# display output from hubdb ( i get the JSON string, but the foreach values are empty)

{{objects}}<br><br>

{# CONVERT THE JSON STRING TO AN OBJECT USING A HUBL FILTER SO YOU CAN ITERATE THROUGH IT #}
{% set altObjects = objects|fromjson %}
 {{altObjects}}<br><br>

{#display  output from test hardcoded array (this works, but not the method what i want) #}
{{objects2}}<br><br>

 

{# Load  textvalue and urlvalue from the hubdb column (where the JSON text is inserted) #}
{% for obj in altObjects %}

Title: {{obj.textvalue}} <br>Slug: {{obj.urlvalue}}<br><br>

{% endfor %}

 

 

 

 

 

this is the bit that you needed, to set a new variable that has the object from hubdb parsed from json:

 

{% set altObjects = objects|fromjson %}

 

 

Here is the example a bit modified but much more readable:

{% set objects = module.row.columns['kleur_decibel_red_structuur_mko'] %}
{% set altObj = objects|fromjson %}

{% for obj in altObj %}
  {{obj.textvalue}}<br>
  {{obj.urlvalue}}<br>
{% endfor %}

 

 

If my answer helped you solve your issue please mark it as an accepted solution.

 

Voir la solution dans l'envoi d'origine

5 Réponses
webdew
Guide | Partenaire solutions Diamond
Guide | Partenaire solutions Diamond

How to get a JSON from a hubdb column and use in hubl

Résolue

Hi @TDwebdev ,

Use these command and fetch and watch your src: https://prnt.sc/1eoy3nff

hs watch src foldername
npx hs fetch foldername src


Hope this helps!


If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards. 

0 Votes
miljkovicmisa
Contributeur de premier rang | Partenaire solutions Platinum
Contributeur de premier rang | Partenaire solutions Platinum

How to get a JSON from a hubdb column and use in hubl

Résolue

Hello @TDwebdev , could you elaborate a bit more with this example? Maybe show us the output you'd like to achieve, I'm struggling to understand what you need to accomplish.
Thanks!

0 Votes
TDwebdev
Contributeur | Partenaire solutions Diamond
Contributeur | Partenaire solutions Diamond

How to get a JSON from a hubdb column and use in hubl

Résolue

Hello @miljkovicmisa  , I updated the post!



Vet Digital

Did my post solve your question? Help the community by marking it as a solution
0 Votes
miljkovicmisa
Solution
Contributeur de premier rang | Partenaire solutions Platinum
Contributeur de premier rang | Partenaire solutions Platinum

How to get a JSON from a hubdb column and use in hubl

Résolue

Okay, so now I got it, since json is in the field it is parsed as json string when you assign it to a variable, thus you need to use the fromjson filter in order to reconvert it to an object when read from the hubdb. More info on that filter here.

 

So your code should look something like this (I added my comment in all caps, you basically needed to convert json to object because hubl is not parsing json but hubl objects):

 

 

 

 

{# Example test array (this works) #}
{% set objects2 = [{"textvalue":"Thorngrijs 11-05 Newtone", "urlvalue":"thorngrijs-11-05-newtone"},{"textvalue":"Rood 01-76 Newtone", "urlvalue":"rood-01-76-newtone"}] %}

 

{# display output from hubdb ( i get the JSON string, but the foreach values are empty)

{{objects}}<br><br>

{# CONVERT THE JSON STRING TO AN OBJECT USING A HUBL FILTER SO YOU CAN ITERATE THROUGH IT #}
{% set altObjects = objects|fromjson %}
 {{altObjects}}<br><br>

{#display  output from test hardcoded array (this works, but not the method what i want) #}
{{objects2}}<br><br>

 

{# Load  textvalue and urlvalue from the hubdb column (where the JSON text is inserted) #}
{% for obj in altObjects %}

Title: {{obj.textvalue}} <br>Slug: {{obj.urlvalue}}<br><br>

{% endfor %}

 

 

 

 

 

this is the bit that you needed, to set a new variable that has the object from hubdb parsed from json:

 

{% set altObjects = objects|fromjson %}

 

 

Here is the example a bit modified but much more readable:

{% set objects = module.row.columns['kleur_decibel_red_structuur_mko'] %}
{% set altObj = objects|fromjson %}

{% for obj in altObj %}
  {{obj.textvalue}}<br>
  {{obj.urlvalue}}<br>
{% endfor %}

 

 

If my answer helped you solve your issue please mark it as an accepted solution.

 

dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

How to get a JSON from a hubdb column and use in hubl

Résolue

@Anton , @piersg , @tjoyce  this is an interesting question.  Any thoughts?

@TDwebdev , whatchya doin here 👀

0 Votes