Home > Software engineering >  How to search array in array with dynamic variables using yq
How to search array in array with dynamic variables using yq

Time:06-27

I am having json file as sample.json

[
  {
    "id": "1",
    "name": "default",
  },
  {
    "id": "2",
    "name": "name2",
  },
  {
    "id": "3",
    "name": "name3",
  }
]

Another YAML file(target.yaml)

-
  name: default
  webhookurl: "http://localost:9091/te"
-
  name: default1
  webhookurl: "http://localost:9091/tee"

I wanted to get final result as data from YAML file and perform contains operation on data from sample.json file. I tried with following way yq e '. | .[].name | contains("default","default1")' sample.json This is giving result as follow:- Note: sample.json is nothing but in image refers to myref.txt enter image description here

Another approach tries is myarr=$(yq e '. | .[].name ' target.yaml) # Put array into myarr variable yq e '. | .[].name | contains(myarr)' sample.json # This wont work yq e '. | .[].name | contains(env(myarr))' sample.json # This wont work either

Expected results should be

default -> True
default1 -> false

CodePudding user response:

Given the two files and using kislyuk/yq this would work:

yq -y '(input | map(.name)) as $names | map(.name as $name | {($name): any($names[]; . == $name)})' target.yaml sample.json
- default: true
- default1: false
  • Related