I try to transform a .yml
file to .csv
and I cannot managed to perform the following operation on this file.
I want to extract all the properties name
of each element, in a single column, separated by a character (not comma ,).
[
{
"id": "6426edc70426309fa3bb0ccd",
"labels": [
{
"id": "6425a8e74720c87ca4219d8f",
"idBoard": "xxx",
"name": "label-red-label",
"color": "red"
},
{
"id": "642aa09cd201f3dd2e12b6f6",
"idBoard": "xxx",
"name": "label-green-label",
"color": "green"
}
],
"idLabels": [
"6425a8e74720c87ca4219d8f",
"642aa09cd201f3dd2e12b6f6"
],
"name": "titre de la carte 1",
"url": "https://trello.com/c/url-carte-1"
},
{
"id": "6426edc70426309fa3bb0eef",
"labels": [
{
"id": "6425a8e74720c87ca4219d8f",
"idBoard": "xxx",
"name": "label-red-label",
"color": "red"
}
],
"idLabels": [
"6425a8e74720c87ca4219d8f"
],
"name": "titre de la carte 2",
"url": "https://trello.com/c/url-carte-2"
},
{
"id": "6426edc70426309fa3bb0dde",
"labels": [
{
"id": "642aa09cd201f3dd2e12b6f6",
"idBoard": "xxx",
"name": "label-green-label",
"color": "green"
}
],
"idLabels": [
"642aa09cd201f3dd2e12b6f6"
],
"name": "titre de la carte 3",
"url": "https://trello.com/c/url-carte-3"
}
]
sample.json
This is the result I wish to get (with a command I couldn't find yet):
id_card,name,labels
6426edc70426309fa3bb0ccd,titre de la carte 1,"label-red-label;label-green-label"
6426edc70426309fa3bb0eef,titre de la carte 2,"label-red-label"
6426edc70426309fa3bb0dde,titre de la carte 3,"label-green-label"
I didn't manage to use the | join(";")
properly, but it might be a solution
With the following command:
docker run --rm -v "${PWD}":/workdir mikefarah/yq -o=csv '[[ "id_card", "name", "labels"]] + [.[] | [ .id, .name, .labels.[].name ]]' sample.json
I get the following result:
id_card,name,labels
6426edc70426309fa3bb0ccd,titre de la carte 1,label-red-label,label-green-label
6426edc70426309fa3bb0eef,titre de la carte 2,label-red-label
6426edc70426309fa3bb0dde,titre de la carte 3,label-green-label
This is almost correct but the names of the labels are splitted by a comma , so my CSV file is not valid anymore.
Do you have any idea how I could fix this ?
I found that using [.labels.[].name] | join(";")
works to put a ;
between each element.
The full command:
docker run --rm -v "${PWD}":/workdir mikefarah/yq -o=csv '[[ "id_card", "name", "labels"]] + [.[] | [ .id, .name, [.labels.[].name] | join(";") ]]' sample.json