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
JSON objects are written in key/value pairs. JSON objects are surrounded by curly braces { } . Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon.
In order to set a key-value pair in a KiiObject, call the set() method of the KiiObject class. The set() method has an overloaded version for each data type. Specify a key-value pair as arguments of the set() method. The specified key-value pair will be saved at the first level of the JSON document hierarchy.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With