CMS Development

TDwebdev
投稿者 | Diamond Partner
投稿者 | Diamond Partner

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

解決

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 いいね!
1件の承認済みベストアンサー
miljkovicmisa
解決策
トップ投稿者 | Platinum Partner
トップ投稿者 | Platinum Partner

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

解決

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.

 

元の投稿で解決策を見る

5件の返信
webdew
ガイド役 | Diamond Partner
ガイド役 | Diamond Partner

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

解決

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 いいね!
miljkovicmisa
トップ投稿者 | Platinum Partner
トップ投稿者 | Platinum Partner

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

解決

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 いいね!
TDwebdev
投稿者 | Diamond Partner
投稿者 | Diamond Partner

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

解決

Hello @miljkovicmisa  , I updated the post!



Vet Digital

Did my post solve your question? Help the community by marking it as a solution
0 いいね!
miljkovicmisa
解決策
トップ投稿者 | Platinum Partner
トップ投稿者 | Platinum Partner

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

解決

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
HubSpot製品開発チーム
HubSpot製品開発チーム

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

解決

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

@TDwebdev , whatchya doin here 👀

0 いいね!