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

SOLVE
TDwebdev
Participant | Diamond Partner

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.

0 Upvotes
1 Accepted solution

Accepted Solutions
miljkovicmisa
Solution
Contributor | Gold Partner

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.

 

View solution in original post

5 Replies 5
dennisedson
Community Manager

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

@TDwebdev , whatchya doin here 👀

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
miljkovicmisa
Contributor | Gold Partner

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 Upvotes
TDwebdev
Participant | Diamond Partner

Hello @miljkovicmisa  , I updated the post!

0 Upvotes
miljkovicmisa
Solution
Contributor | Gold Partner

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.

 

View solution in original post

webdew
Top Contributor | Diamond Partner

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 Upvotes