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 %}
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.
jul 23, 20213:45 AM - editado jul 23, 20215:25 AM
Top colaborador(a) | Parceiro Platinum
How to get a JSON from a hubdb column and use in hubl
resolver
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.
How to get a JSON from a hubdb column and use in hubl
resolver
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!
jul 23, 20213:45 AM - editado jul 23, 20215:25 AM
Top colaborador(a) | Parceiro Platinum
How to get a JSON from a hubdb column and use in hubl
resolver
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.