Lyhyt johdatus YAML-kieleen
YAML:ia käytetään kuvaamaan avain-arvo -karttoja ja taulukoita. YAML-tiedostot tunnistetaan käyttämällä .yml
tai .yaml
tiedostopäätettä.
YAML-datasetti voi olla
- arvo
- taulukko
tai
- sanakirja
tai
- YAML-datasetti
key:
- value 1
- another key:
yet another key: value 2
another key 2:
- more values
this keys value is also an array:
- but indentation is not necessary here
Arvot voidaan syöttää monille riveille käyttäen >
:
key: >
Here's a value that is written over multiple lines
but is actually still considered a single line until now.
Placing double newline here will result in newline in the actual data.
Verbatim-tyyliä tuetaan |
merkillä:
YAML vs JSON
YAML on JSON:in (JavaScript Object Notation) yläjoukko. Tällöin,
{
"key": [
{
"value 1": {
"another key": {
"yet another key": "value 2"
},
"another key 2": [
"more values"
],
"this keys value is also an array": ["but indentation is not necessary here"]
}
}
]
}
on myös kelvollinen YAML. Yleisesti ottaen YAML on kompaktimpaa kuin JSON:
key:
- value 1:
another key:
yet another key: value 2
another key 2:
- more values
this keys value is also an array:
- but indentation is not necessary here
yq komentorivityökalu
yq
on hyödyllinen työkalu yaml
-tiedostojen kanssa työskentelyyn. Sen voi asentaa käyttämällä pip
:
$ pip show yq
Name: yq
Version: 3.0.2
Summary: Command-line YAML/XML processor - jq wrapper for YAML/XML documents
Home-page: https://github.com/kislyuk/yq
Author: Andrey Kislyuk
Author-email: kislyuk@gmail.com
License: Apache Software License
Location: /home/jtahir/Documents/csc-stuff/osclient/lib/python3.6/site-packages
Requires: argcomplete, PyYAML, toml, xmltodict
Required-by:
yq
on jq
-kääre. Tämä tarkoittaa, että se muuntaa yaml-syötteen jsoniksi ja käsittelee sen sen jälkeen jq
:lle, ja siksi sillä on sama syntaksi kuin jq
:lla. Seuraavassa esimerkissä .data.WebHookSecretKey
:n arvo haetaan raakatilassa (ilman lainausmerkkejä):