Say I have a yaml file, example.yaml:
networks:
  net:
    driver: overlay
dockerfiles:
  nodeapp: |
    FROM node:18-alpine
    WORKDIR /$NAME
services:
  registry:
    image: registry:2
    ports:
      - "5000:5000"
      - "337:337"
  pinger:
    build:
      context: .
      dockerfile: nodeapp
    deploy:
      replicas: 3
I can convert to JSON using yq, and then set the top level entries as variables with jq:
eval $(yq e example.yaml -j | jq -r '..? | to_entries | .[] | .key + "=" + (.value|tostring)')
echo $services
{registry:{image:registry:2,ports:[5000:5000]},pinger:{build:{context:.,dockerfile:nodeapp},deploy:{replicas:3}}}
However, I cannot seem to get jq to recursively go into the objects and set variables, so that echo $services_pinger_dockerfile gives "nodeapp" for example.
I've seen in the docs that there is a recursion operator. Can anyone suggest a way of doing this?
There are similar questions. But none that I found dealing with multilevel YAML/JSON to shell variables in this way.
jq: Getting two levels of keys
Extract JSON value to shell variable using jq
https://unix.stackexchange.com/questions/413878/json-array-to-bash-variables-using-jq
https://unix.stackexchange.com/questions/121718/how-to-parse-json-with-shell-scripting-in-linux
 
    