Skip to content

Flatten an array in substation v1.1.0 #155

Answered by jshlbrd
viraj-lunani asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @viraj-lunani, the flatten processor wasn't migrated to v1.0 since it's supported natively by Substation's JSON parser (GJSON). You can flatten {"b":["val3","val4",["val1","val2"]]} by using a copy transform and the GJSON @flatten modifier. Try this:

sub.tf.object.copy({object: {source_key: 'b|@flatten', target_key: 'b'}})

If you need to extend often, then you could build a custom transform Jsonnet function like:

extend(source, target): [
  // This appends the value from `source` to the end of `target`.
  sub.tf.object.copy({object: {source_key: source, target_key: target + '.-1' }}),
  sub.tf.object.copy({object: {source_key: target + '|@flatten', target_key: target}}),  
],

If you w…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@viraj-lunani
Comment options

@jshlbrd
Comment options

Answer selected by viraj-lunani
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