'How do I parse external JSON file in a Helm _helpers.tpl

I am writing a Helm _helpers.tpl file. This helper needs to

  1. read a JSON value from a file not in the yaml/values of the charts.
  2. Use variables in the charts/values/yaml to determine which field of the external JSON to read
  3. store the value extracted from the JSON into a local Go variable
  4. combine the values if the Go variable and the chart variables to output into a final value.

My external JSON file looks like this:

{
  "java": {
    "8": {
      "version": "0.1.8"
    },
    "11": {
      "version": "0.1.11"
    }
  },
  "node": {
    "14": {
      "version": "14.5.0"
    },
    "16": {
      "version": "16.4.0"
    }
  }
}

I have the following variables at my disposal in my values /Charts

  • .Values.type
  • .Values.typeVersion

my _helpers.tpl looks like this:

{{- $imageversions := (.Files.Get "../../../../common/versions.json" | toJson | jq ".".Values.type".".Values.typeVersion"."version) -}}
{{- printf "artifactory.myco.com/docker/%s/ubuntu20-slim-%s%s.0f:%s" .Values.type .Values.type .Values.typeVersion $imageversions }}

The first line of this code (above) is where I am needing help. Currently, I

  • use .Files.Get to extract the file contents
  • ensure it is interpretted as JSON by using toJson
  • try to read the specific field I am interested using jq
  • assign local variable $imageversions (far left) to the value found in the JSON

I think I have everything ok, except I don't have jq on this computer. How can I parse the JSON and get the value I need in this Helm Go template helper?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source