Skip to content

How to set target_key to a variable value #197

Answered by jshlbrd
nishaprabhakar asked this question in Q&A
Discussion options

You must be logged in to vote

@nishaprabhakar Since your JSON schema is consistent, this will work for what you want to do:

local sub = import '../../../../../build/config/substation.libsonnet';

// invert_kv copies a nested value and moves it to the top 
// level as a key.
local invert_kv(keys) = [
  // The key is interpolated into the nested path.
  local key_string_val = std.format('attributes.#(key==%s).value.stringValue', key);

  // The source is the location of the nested value.
  // The target is the key input with dots escaped.
  sub.tf.object.copy({ object: { source_key: key_string_val, target_key: std.strReplace(key, '.', '\\.') } }),

  // This for-loop must be at the bottom of the function.
  for key in k…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@nishaprabhakar
Comment options

@jshlbrd
Comment options

@jshlbrd
Comment options

Answer selected by nishaprabhakar
@nishaprabhakar
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants