Reputation: 834
Let's say I have the following specific YAML file:
task:
container:
image: ubuntu:latest
args: tail
args: -f
args: /dev/null
mounts:
source: /home/testVolume
target: /opt
using the ruby command ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))' test.yml > testTab.json
I get only the last args printed:
"task": {
"container": {
"image": "ubuntu:latest",
"args": "/dev/null",
"mounts": {
"source": "/home/testVolume",
"target": "/opt"
}
My question is how to get all the three args printed instead of only the last one?
Upvotes: 0
Views: 639
Reputation: 39708
Your YAML is invalid. Quoting from the YAML spec:
JSON's RFC4627 requires that mappings keys merely “SHOULD” be unique, while YAML insists they “MUST” be. Technically, YAML therefore complies with the JSON spec, choosing to treat duplicates as an error. In practice, since JSON is silent on the semantics of such duplicates, the only portable JSON files are those with unique keys, which are therefore valid YAML files.
It is an error to have multiple identical keys in a YAML mapping. However, JSON allows it since it only states that they SHOULD be unique. Therefore, the resulting JSON would be valid if an implementation supports it, but beware that it is not required to.
The answer to your question is: It does not work because your input is not valid YAML. Choose a structure that is representable in YAML, then it will work. For example:
task:
container:
image: ubuntu:latest
args:
- tail
- -f
- /dev/null
mounts:
source: /home/testVolume
target: /opt
Resulting JSON:
{
"task": {
"container": {
"image": "ubuntu:latest",
"args": [
"tail",
"-f",
"/dev/null"
],
"mounts": {
"source": "/home/testVolume",
"target": "/opt"
}
}
}
}
Upvotes: 2