sheeks06 sheeks06 - 24 days ago 7
JSON Question

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

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

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
Comments