Lechucico Lechucico - 20 days ago 7
JSON Question

JQ: Generate txt file with json information

I have the following Json:

[{
"_id": {
"$oid": "59c3942baeef22b03fa573d2"
},
"client_id": "test@test.com",
"name": "Windows Client"
},
{
"_id": {
"$oid": "59c3942baeef22b03fa573d2"
},
"client_id": "test2@test2.com",
"name": "Linux Client"
}]


I would like to output the following:

mongo_customers,customer=test@test.com
mongo_customers,customer=test2@test.com


I've tryed the following but it doesn't work:

jq -n -R \
--slurpfile mongo mongo.json \
'
$mongo[][].client_id] as $mongo_ids
"mongo_customers,customer="($mongo_ids)
'


How I can do that?

Answer Source
jq -r '.[] | "mongo_customers,customer=\(.client_id)"' mongo.json

There are many other possibilities. If you want to be absolutely sure the result will be valid CSV, then you might want to use the @csv filter.