Lechucico Lechucico - 5 months ago 46
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:


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

jq -n -R \
--slurpfile mongo mongo.json \
$mongo[][].client_id] as $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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download