Sebastien Lorber
Sebastien Lorber

Reputation: 92210

Yaml: can we apply a prefix to a whole yaml file/document?

I have a document like this:

---
prefix1:
  prefix2:
    a:
      b:
        a: 'x'
      c:
        b: 'y'
      d:
        c: 'z'
    b:
      b:
        a: 'x'
      c:
        b: 'y'
      d:
        c: 'z'
    c:
      b:
        a: 'x'
      c:
        b: 'y'
      d:
        c: 'z'

The whole document will be nested under prefix1.prefix2 and for various reasons I can't change that easily (different tools using Yaml and the same document as source)

Is there a way to rewrite this document so that I don't have as much indentation to handle?

Something like this?

DocumentPrefix=prefix1.prefix2
---
a:
  b:
    a: 'x'
  c:
    b: 'y'
  d:
    c: 'z'
b:
  b:
    a: 'x'
  c:
    b: 'y'
  d:
    c: 'z'
c:
  b:
    a: 'x'
  c:
    b: 'y'
  d:
    c: 'z'

Upvotes: 0

Views: 284

Answers (1)

flyx
flyx

Reputation: 39778

YAML cannot do data transformation, so what you want to do is not possible. If the indentation bugs you, you can use flow-style collections:

---
{ prefix1: { prefix2: {
a: {
  b: {a: 'x'},
  c: {b: 'y'},
  d: {c: 'z'}
},
b: {
  b: {a: 'x'},
  c: {b: 'y'},
  d: {c: 'z'},
},
c: {
 b: {a: 'x'},
 c: {b: 'y'},
 d: {c: 'z'}
}
}}}

Upvotes: 2

Related Questions