简体   繁体   中英

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 ie HOST-0 and HOST-1 without specifying them or if anyone has any ideas

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM