'jsonnet std.map - "attempted to index array with string name" error

i am new to jsonnet

I am trying to call the name and nb seperately for different operations. But i am getting error "attempted to index array with string name" when i use "specificValues.data.LIST.name" in the code.

    local NbMap = { ABC: 1, XYZ: 4 },
    
    LIST: std.map(function(key_name) {
      name: key_name,
      nb: NbMap[key_name],
    }, std.objectFields(NbMap)),

local specificParameters = {
  ConfigMap: {
    'data': {
       connect: [
{                     ...
                      name: 'ALL',
                      ...
                      filter: std.join(';', std.map(function(name) name + ':NAM==[' + name + ']', specificValues.data.LIST.name)),
                      ...
                    },
                  ]
                  +
                  [
                    {
                      Max: specificValues.data.LIST.nb / 5,
                      ...
                      name: NbMap.name,
                      ...
                    }
                    for name in specificValues.data.LIST.name
                  ],
    },
  },

I am not sure how to call the indexes in jsonnet. Please let me know if anyone knows.



Solution 1:[1]

1st a bit of relevant feedback: you should really provide a narrowed-down usable excerpt derived from your use case, else it's an extra effort from people trying to help you, which is obviously unfair for them/us.

The main problem here is that you're somehow assuming that you can loop over e.g. LIST.name, which is not the case with jsonnet: the only possible comprehensions are over arrays: [entry for entry in array] (array here could also be std.objectFields(obj) or std.objectValues(obj) if you're dealing with objects in the 1st place).

Pasting below a possible solution (code and manifested output) for what I think you want to achieve:

code

local NbMap = { ABC: 1, XYZ: 4 };

local specificValues = {
  data: {
    LIST: std.map(function(key_name) {
      name: key_name,
      nb: NbMap[key_name],
    }, std.objectFields(NbMap)),
  },
};

local specificParameters = {
  ConfigMap: {
    data: {
      connect: [
        // Loop over each LIST entry to generate `connect` string
        {
          name: 'ALL',
          filter: std.join(';', std.map(function(x) x.name + ':NAM==[' + x.name + ']', specificValues.data.LIST)),
        },
      ] + [
        // Loop over each LIST entry to add {Max: .., name: ...}
        {
          Max: entry.nb / 5,
          name: entry.name,
        }
        for entry in specificValues.data.LIST
      ],
    },
  },
};

// Show specificParameters manifest rendering
specificParameters

output

{
   "ConfigMap": {
      "data": {
         "connect": [
            {
               "filter": "ABC:NAM==[ABC];XYZ:NAM==[XYZ]",
               "name": "ALL"
            },
            {
               "Max": 0.20000000000000001,
               "name": "ABC"
            },
            {
               "Max": 0.80000000000000004,
               "name": "XYZ"
            }
         ]
      }
   }
}

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 jjo