Tips on extracting specific information from a JSON object within an array using a key from a different object in Hugo

I am currently working with a data file in Hugo that contains information structured like the following:

{
  "schedule": {
    "week1": [
      {
        "day": 0,
        "away": 1,
        "home": 2,
        "completed": false,
        "score": {
          "away": [
            0,
            0,
            0,
            0
          ],
          "home": [
            0,
            0,
            0,
            0
          ]
        },
        "time": "6:15"
      },
      etc.
    ]
  },
  "teams": [
    {
      "uid": 0,
      "name": "Buffalo",
      "conference": 0,
      "division": 0,
      "tla": "BUF",
      "offense": 10,
      "defense": 10,
      "wins": 0,
      "loses": 0,
      "ties": 0
    },
    etc.
  ]
}

In my code, I have implemented a range loop to iterate through each game in week one. My aim is to extract the ID of the home team and then find the corresponding team within the teams array.

Specifically, for

.Site.Data.rfl_data.schedule.week1
, if we locate game one and retrieve the .home value, we can use it to index into the teams array. In JavaScript syntax, this operation would resemble
data.teams[data.schedule.week1[1].home].name
. This means that if the home value was 0, the output would display the name of the team at the 0th position in the teams array. My goal now is to achieve this functionality within Hugo by accessing and manipulating the necessary data.

Answer №1

If you're interested, check out https://gohugo.io/functions/index-function/ .

Feel free to use this code snippet:

<ul>
{{ range .Site.Data.test.schedule.week1 }}
  <li>
    home: {{ .home }},
    team name: {{ index $.Site.Data.test.teams (.home | int) "name" }}
  </li>
{{ end }}
</ul>

Note: .home is considered float64, so converting it to int may be necessary.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When the JSON array is converted into a string, it appears as undefined

Below is a snippet of my service.spec.ts file: service.spec.ts it('should mock the http requests', inject([Service, MockBackend], (service, mockBackend) => { let result:any; mockBackend.connections.subscribe((connection) => { ...

executing jQuery toggle on freshly generated content

I have a webpage that I designed using jQuery. Within this page, there is a table with rows assigned specific color classnames (e.g., tr class="yellowclass"). Users can filter the rows by clicking checkboxes to show/hide tables of different colors. The i ...

Remove quotation marks from numbers in JSON Builder

I am facing an issue while running the groovy scripts below to create a JSON template. The problem lies in the fact that the integer values in the template are displayed within quotes, treating them as strings. cat port.txt 1001 Below is the JSON builder ...

determining the position of the substring located within an "if any" function

I have two lists A and B. My goal is to extract a string from list A, find a matching string in list B, and then determine the index of that matching string in list B. I've attempted several nested loops without success. I haven't come across an ...

ngModel Error: Unable to retrieve the 'name' property of an undefined value

I have a JSON file that displays different levels of data, some in regular format and some as arrays as shown below. [![enter image description here][1]][1] However, I keep encountering an error message like the one below: [![enter image description her ...

ExpressJs res.json throwing error - Headers cannot be set after they have already been sent

In my current project using ExpressJS, I have a specific route set up like this: router.route('/monitor') .all(function (req, res, next) { next(); }).get(monitor.monitorServers); There is also a controller named 'monitor' which co ...

Unraveling an enum with boolean values

I'm currently working with a JSON file that contains information about holidays. [ { "name": "January", "holidays": [ { "name": "New Year's Day", "date": "2019-01-01T00:00:00-0500", ...

Is it possible for Angular2 to map a lone JSON object?

Dealing with a JSON response that is a single object, rather than an array, can be tricky. Recently in my project, I encountered a situation where I needed to map and use such a response from an API to fill out a template. It seemed like a simple task at f ...

Creating JSON Objects in PHP

How Can I Generate Similar Json Data Using PHP? [ [ "16226281", "11", "Disclosure.1994.720p.BluRay.H264.AAC-RARBG", "finished" ], [ "16226038", "140", "Courage The Cowardly Dog (1999-2002)", "finished" ], [ "16226020", ...

A guide on achieving a dynamic color transition in Highcharts using data values

I am currently working on plotting a graph using high charts and I would like to change the color based on the flag values. I attempted this, however, only the points are changing based on the flag values and the line is not being colored accordingly. Be ...

Methods for eliminating curly braces from HTTP response in JavaScript before displaying them on a webpage

When utilizing JavaScript to display an HTTP response on the page, it currently shows the message with curly braces like this: {"Result":"SUCCESS"} Is there a way to render the response message on the page without including the curly braces? This is the ...

Guide on incorporating a JSON dictionary into realm using AlamofireObjectMapper

I have encountered an issue while trying to insert my JSON objects into Realm. The error message that keeps popping up is: Could not cast value of type '__NSCFDictionary' (0x10c52f178) to 'NSArray' (0x10c52eb88). The error seems to ...

An issue has arisen while parsing a JSON array that is set up to accept

My code works perfectly fine when I provide a single data element in my JSON file. However, when I input an array of elements, it starts displaying "undefined" on the client side. Below is the snippet of my server-side code: var app = require('expres ...

I am facing difficulties in adding dynamic content to my JSON file

I've encountered a challenge in appending new dynamic data to a JSON file. In my project, I receive the projectName from an input form on the /new page. My API utilizes node.js's fs module to generate a new JSON file, where I can subsequently add ...

Generate an array of JavaScript objects by converting a batch of JSON objects into objects within a Node.js environment

I am working with a prototype class: function customClass(){ this.a=77; } customClass.prototype.getValue = function(){ console.log(this.a); } and I also have an array of JSON objects: var data=[{a:21},{a:22},{a:23}]; Is there a way to cre ...

Updating data from the database in real-time

This web application needs to constantly retrieve any new data added into the database and display it instantly without requiring a full page refresh. It's essentially a live update feature! :) ...

Tips on transforming a JSON array object into a JSON array

**Background:** I have been utilizing lodash to eliminate the empty key from my JSON data. However, upon removal of the keys, it transforms my array into an object. For instance: { "projection": "Miller", "series": [ { "mapPolygons": { ...

How to eliminate an element from numerous arrays using jQuery?

Is it possible to use jq to remove all instances of a specific name from arrays within the input data? For example, removing "Name1" from the following: { "Category1": [ { "name": "Name1", "desc": "Desc1" }, { "name": "Name ...

Using angular.copy function to copy an array with a custom property

Let's use the example below to illustrate an issue: var ar = [4, 2, 3]; ar.$x = 'something'; var br = angular.copy(ar); console.dir(br); After copying ar to br, the $x property is no longer present. This is because when Angular copies an a ...

How can I use Presto/Athena to find the frequency of JSON attributes in a query?

I created a Hive table with a single column that stores JSON data: CREATE EXTERNAL TABLE IF NOT EXISTS my.rawdata ( json string ) PARTITIONED BY (dt string) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' WITH SERDEPROPERTIES ( ...