Skip to content

Merging array elements with two different schemas #968

Answered by mikefarah
mssanjay asked this question in Q&A
Discussion options

You must be logged in to vote

TLDR:

yq ea '
  (select(fi==0) | .mgwaskong.kong.dblessConfig.config.services) = (select(fi==1) | .services) | 
  select(fi==0)
' examples/data1.yaml examples/data2.yaml

Explanation:

  • use ea (eval-all) to load all file in memory.
  • select the first file select(fi==0), pipe into the path you want to update .mgwaskong.kong.dblessConfig.config.services. This is in brackets so it becomes the LHS of the = operator
  • on the RHS, we select the second file and path we want to set.
  • after the update, we filter context to only include the first file

Note that in place of = you can use += if you want to append to an existing array, * to merge etc.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mikefarah
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