35

say if you have a file called example.yaml which contains the following: - subject: maths.

How do i grab the string after - subject?

I already can read the contents of the file but want to know how to grab a specific string from it.

note: i know regex may help but have never used it and would appreciate any help.

Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
user3412172
  • 489
  • 2
  • 6
  • 10

2 Answers2

48

I am adding here the same example, but implemented with the native YAMLSlurper (Groovy 3.x+):

import groovy.yaml.YamlSlurper

def exampleYaml = '''\
---
- subject: "maths"
- subject: "chemistry"
'''

List example = new YamlSlurper().parseText(exampleYaml)
// If your source is a File
// List example = new YamlSlurper().parse("example.yaml" as File)

example.each{println it.subject}

For previous versions (Original answer):

snakeyaml is a library to parse YAML files. Easy to use in groovy.

UPDATE: changed type of the example variable to List, as the example file's top level element is a collection

@Grab('org.yaml:snakeyaml:1.17')

import org.yaml.snakeyaml.Yaml

Yaml parser = new Yaml()
List example = parser.load(("example.yaml" as File).text)

example.each{println it.subject}

Full documentation of snakeyaml:

https://bitbucket.org/snakeyaml/snakeyaml/wiki/Documentation

Saikat
  • 14,222
  • 20
  • 104
  • 125
Luis Muñiz
  • 4,649
  • 1
  • 27
  • 43
  • 1
    Getting this error: Cannot cast objetct '[{subject=maths}]'with class 'java.util.ArrayList' to class 'java.util.Map' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.Map(java.util.LinkedHashMap, java.util.LinkedHashMap) org.codehaus.groovy.runtime.typehandling.GroovyCastException: – user3412172 Jan 19 '17 at 12:24
  • Can you post a representative sample of your yaml file? – Luis Muñiz Jan 19 '17 at 13:22
  • `--- - subject: maths` (new line) – user3412172 Jan 19 '17 at 13:32
  • I changed the type of the example variable. if your yaml was of the form x:y it would result in a map. Your yaml however has the form -x:y. This is a collection of maps – Luis Muñiz Jan 19 '17 at 14:09
  • how would i go about making this recursive? say define an endpoint folder to start at and have it read all subdirectories? – user3412172 Jan 21 '17 at 21:04
  • 1
    this link https://bitbucket.org/asomov/snakeyaml/wiki/Documentation is not accessible!! – U.V. Nov 25 '21 at 16:52
  • True, I will update my answer to include an implementation with YAMLSlurper – Luis Muñiz Dec 11 '21 at 19:02
  • New link: https://bitbucket.org/snakeyaml/snakeyaml/wiki/Documentation – Saikat Feb 11 '23 at 14:40
20

FWIW, the upcoming (as time of this writing) Groovy version 3.0 has direct support for yaml: http://docs.groovy-lang.org/next/html/api/groovy/yaml/package-summary.html with the traditional YamlSlurper / YamlBuilder combo You could always switch to this not-yet-officially-released version.

[Edited] that version 3.0.x is now officially available, with the groovy.yaml package http://docs.groovy-lang.org/latest/html/api/groovy/yaml/package-summary.html

Patrice M.
  • 4,209
  • 2
  • 27
  • 36
  • Unfortunately even a late Jenkins [v2.326](https://www.jenkins.io/changelog/#v2.326) uses Groovy [2.4.21](https://groovy-lang.org/changelogs/changelog-2.4.21.html) released more than a year ago. – Boris Mar 29 '22 at 19:35
  • 3
    @Boris yes indeed, though you can now use the built-in function `readYaml` https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readyaml-read-yaml-from-files-in-the-workspace-or-text – Patrice M. Mar 29 '22 at 22:56