mitch mitch
mitch mitch

Reputation: 1

Groovy YAML parsing

I am trying to parse some yaml from a file with Groovy here is what I currently have:

import groovy.yaml.YamlSlurper
def yamlFile = new File('./test.yaml')
def inputYaml = new YamlSlurper().parse(yamlFile)
inputYaml.each{ println it }

The above prints the yaml file, also below is my yaml file

HOST-0:
  tags:
    name: 'HOST0'
    size: 'small'
    region: 'west'
HOST-1:
  tags:
    name: 'HOST0'
    size: 'small'
    region: 'west'

What I'm wonder is if it is possible to create a separate yaml object that would lookup certain values, so lets say I wanted to create a new yaml file that would look like:

HOST-0:
  hostname: 'HOST0'

Normally I would be able to do a lookup which would lookup which would look something like HOST-0:tags:host just wondering if it's possible to do this sort of lookup on each yaml object i.e HOST-0 and HOST-1 without specifying them or if anyone has any ideas

Upvotes: 0

Views: 1677

Answers (1)

daggett
daggett

Reputation: 28564

def outYaml = inputYaml.collectEntries{ k,v-> [k, [hostname:v.tags.name] ] }

Upvotes: 1

Related Questions