Can't get key/value from hash
The doc states
"When iterating a hash, item[0] contains the key, and item[1] contains the value" https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
But i'm getting blanks. I'm testing here: https://pramodvalavala-msft.github.io/liquid-playground/
Template:
{
{%- for item in answers -%}
{%- assign oSubAnswers = item.answer -%}
{{oSubAnswers}}
{%- for subAns in oSubAnswers -%}
{{ subAns[0] }}: {{ subAns[1] }}
{%- endfor -%}
{%- endfor -%}
}
Data:
{
"answers": {
"12": {
"answer": {
"field_1": "John",
"field_3": "White",
"field_5": 1111111,
"field_4": "111-111-1111"
}
},
"22": {
"answer": {
"field_2": "111 Ventura Blvd",
"field_3": "111-111-1111",
"field_8": "[email protected]",
"field_7": "https://tort.xyz",
"field_6": "10 am to 6 pm, M-F"
}
}
}
}
Output:
{
[field_1, John][field_3, White][field_5, 1111111][field_4, 111-111-1111][itemName, answer]
:
:
:
:
:
[field_2, 111 Ventura Blvd][field_3, 111-111-1111][field_8, [email protected]][field_7, https://tort.xyz][field_6, 10 am to 6 pm, M-F][itemName, answer]
:
:
:
:
:
:
}
Hello @johnaweiss.
Your template doesn't account for the shape of data correctly.
Let's say "data" holds the given data. Then your template (to render all info) would be:
{% for item in data.answers %}
Id: {{ item[0] }}
Answer Keys:
{% assign oSubAnswers = item[1]["answer"] -%}
{% for subAns in oSubAnswers -%}
{{ subAns[0] }}: {{ subAns[1] }}
{% endfor %}
{% endfor %}
Result:
Id: 12
Answer Keys:
field_1: John
field_3: White
field_5: 1111111
field_4: 111-111-1111
Id: 22
Answer Keys:
field_2: 111 Ventura Blvd
field_3: 111-111-1111
field_8: [email protected]
field_7: https://tort.xyz
field_6: 10 am to 6 pm, M-F
So, if this is subAns
name : value
Then name is subAns[0], and value is subAns[1] ?
So, : is treated as an array delimiter, rather than key : value pair?
Can an subAns be accessed as a key : value pair?
oSubAnswers['field_8']
You're once again confusing between data structures. JS objects { key: value, name: 'John' } are Hash instances { "key" => "value", "name" => "John" } in Ruby.
When Ruby hashes are iterated through, the data structure is an array of key-value pairs:
[["key", "value"], ["name", "John"]].
So if you were to assess each pair, you see that pair[0] refers to the key, and pair[1] refers to the value.
I think you're saying that Liquid structures are JS objects are equivalent to Ruby Hashes, correct?
Is it possible to use a text-key to retrieve a value from the object, without an explicit loop to test each one?
Or, can the object be converted into a format that can be accessed using text-keys?
oSubAnswers['field_8']
Liquid structures are JS objects are equivalent to Ruby Hashes, correct?
Not at all. Regardless, let's drop that discussion.
use text-key to retrieve..
Yes, we can either use the square-brackets or dot-notation syntax. The reason I outlined my comments with iteration was because your opening post focused on iteration and that it's the best way to render api structures as a whole via Liquid.
Anyways coming back to your query, when given the following JSON:
"comment" : {
"id": 123456,
"author": {
"username": "john_doe",
"email_id": "[email protected]"
},
"body": "Hello World!"
}
Then, I could use Liquid to render comment-author details via either of the following:
Commented by: {{ comment.author.username }}
Commented by:
{{ comment['author']['username'] }}
@ashmaroli i sent a message to the email in your code.