Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get key/value from hash #1810

Open
johnaweiss opened this issue Jun 17, 2024 · 6 comments
Open

Can't get key/value from hash #1810

johnaweiss opened this issue Jun 17, 2024 · 6 comments

Comments

@johnaweiss
Copy link

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": "johnawhite@gmail.com",
          "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, johnawhite@gmail.com][field_7, https://tort.xyz][field_6, 10 am to 6 pm, M-F][itemName, answer]
      : 
      : 
      : 
      : 
      : 
      : 

  }
@ashmaroli
Copy link
Contributor

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: johnawhite@gmail.com
  field_7: https://tort.xyz
  field_6: 10 am to 6 pm, M-F

@johnaweiss
Copy link
Author

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']

@ashmaroli
Copy link
Contributor

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.

@johnaweiss
Copy link
Author

johnaweiss commented Aug 13, 2024

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']

@ashmaroli
Copy link
Contributor

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": "john.doe@example.com"
  },
  "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'] }}

@johnaweiss
Copy link
Author

johnaweiss commented Aug 19, 2024

@ashmaroli i sent a message to the email in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants