YAML multiline wrap without space
Asked Answered
B

2

78

I tried to understand the specifications here but they're actually quite difficult to understand.

http://www.yaml.org/spec/1.2/spec.html#id2779048

As far as I can see, there are three ways of wrapping text but their function is very similar... in fact so similar that I don't get the point in having all of them instead of one or two.

Well my problem is that I have some String that is really long (~700 characters) but has no whitespaces. Now of course I want to put it into multiple lines but there seems to be no way to do so without having any linefeeds or space characters that I do not want.

So is this actually possible?

---
aTest:
    hereComes
    SomeText
    ThatShould
    NotHave
    AnyWhitespaces
Bifocal answered 3/10, 2013 at 20:32 Comment(2)
Possible duplicate of Is there a way to represent a long string that doesnt have any whitespace on multiple lines in a YAML document?Lauer
+1 for "the specifications [...] they're actually quite difficult to understand." still holding true almost 10 years later. Every time I look at it I feel like diving deep down into a doctoral thesis that broke out of the university ivory tower accidentally with a bloodcurdling howl in a moonless, dark night. If RFCs were written like that we'd still use fax only.Speaking
H
88

It's possible. See. Is there a way to represent a long string that doesnt have any whitespace on multiple lines in a YAML document?

Double-quoted example:

"abcdefghi\
jklmnopqr\
stuvwxyz"

Single quotes may also work depending on the parsing library so YMMV.

Harveyharvie answered 3/12, 2014 at 22:21 Comment(4)
See this other StackOverflow answer for a good overview of all the different wrapping styles in YAML.Dannica
Single quotes do not work - you get a literal backslash and newlinePlebiscite
Single quotes worked on a YAML parsing library I was using at the time, may have been a bug in the library perhaps. Sorry about that guysHarveyharvie
I tested with an online tool and worked differently than kustomize (the tool I'm using). Always test with your actual services!Subspecies
C
1

If your yaml is ansible, you can use jinja2 to join the pieces together.

long_list:
    - foo
    - bar
    - baz

long_string: "{{ long_list | join('') }}"

Yields "foobarbaz"

Chelyuskin answered 22/9, 2023 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.