How to convert a JSON object to key=value format in jq?

gianebao picture gianebao · Aug 19, 2014 · Viewed 23k times · Source

In jq, how can I convert a JSON to a string with key=value?

From:

{
    "var": 1,
    "foo": "bar",
    "x": "test"
}

To:

var=1
foo=bar
x=test

Answer

aioobe picture aioobe · Aug 19, 2014

You could try:

jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' test.json

Here's a demo:

$ cat test.json
{
    "var": 1,
    "foo": "bar",
    "x": "test"
}
$ jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' test.json
foo=bar
var=1
x=test