I have a JSON file that looks like this:
{
"key1": "value1",
"key2": "value2",
...
"keyn": "valuen"
}
No arrays or nested objects, just a simple key->value map. I want to use jq
to convert this file to a plain text configuration file of a following format:
KEY1=value1
KEY2=value2
...
KEYn=valuen
(the keys should be uppercased in the result file). I have searched various jq
tutorials available on the Net expecting that such an obvious (in my opinion) example would be covered there (at least without uppercasing), but it is not. All tutorials I have found use more complicated examples, like extracting specific values from JSON file, handling arrays or nested structures etc. However, no simple JSON-to-text conversion. man jq
doesn't help either, it's not too clear. Could you help me in obtaining the result I want?
Note: it must be done with jq
, not any other tool, because it will be used in a script that has access to jq
, but no other tool for handling JSON is guaranteed to be present on the system.
You can iterate over the entries in the object with to_entries
, then format each to a string and use the -r
(--raw-output) flag, like so:
$ cat example.json
{
"key1": "value1",
"key2": "value2",
"keyn": "valuen"
}
$ jq -r 'to_entries[] | (.key | ascii_upcase) + "=" + .value' < example.json
KEY1=value1
KEY2=value2
KEYN=valuen