Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

bash

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 
like image 821
gianebao Avatar asked Aug 19 '14 07:08

gianebao


People also ask

Can a JSON object be a key?

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.

How does JSON create key-value pairs?

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.


1 Answers

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 
like image 149
aioobe Avatar answered Sep 29 '22 09:09

aioobe