Reputation: 13
Have multiple json files (Generated with JQ) that have the following format.
{
"DB": "jaap_35290",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8",
}
I need merged them via jq to have the following format associative array in php (For other systems that need this format that I can't control.)
{
"jaap_35290": {
"DB": "jaap_35290",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8",
},
"db_2": {
"DB": "db_2",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8",
},
}
With jq i am able to merge the json "files" to an normal array but it sadly doesn't work in the other systems...
jq -s <<< $(while read str; do
parse_object_kv_list_json_non_eval "$str"
# output of the first example json file
done < <(cat $USER_DATA/db.conf))
The output will be when phrased by normal array and
{
{
"DB": "jaap_35290",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8",
},
{
"DB": "db_2",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8",
},
}
Also tried:
jq -s <<< $(while read str; do
json=$(parse_object_kv_list_json_non_eval "$str")
key=$(echo $json | jq -r '.DB');
echo "{\"$key\":$json}"
done < <(cat $USER_DATA/db.conf))
But then the output is
[
{
"jaap_35290": {
"DB": "jaap_35290",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8",
}
},
{
"db_2": {
"DB": "db_2",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8",
},
}
]
Any way how I can solve it?
Upvotes: 1
Views: 136
Reputation: 116690
A simple and efficient solution:
jq -n 'INDEX(inputs; .DB)' ...
where ... specifies the relevant file names.
Of course, if you're generating the JSON objects using jq, then you should be able to combine everything into one invocation of jq, using INDEX/2
.
Upvotes: 1
Reputation: 140940
With the following input files:
$ cat /tmp/1.jq
{
"DB": "jaap_35290",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8"
}
$ cat /tmp/2.jq
{
"DB": "db_2",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8"
}
With the help of https://blog.differentpla.net/blog/2019/01/11/jq-reduce/ and https://stedolan.github.io/jq/manual/ I was able to output the expected output with these input files:
$ jq -s 'reduce .[] as $line ({}; . + {($line | .DB): $line} )' /tmp/1.jq /tmp/2.jq
{
"jaap_35290": {
"DB": "jaap_35290",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8"
},
"db_2": {
"DB": "db_2",
"DBUSER": "jaap_35290",
"HOST": "localhost",
"TYPE": "mysql",
"CHARSET": "UTF8"
}
}
Upvotes: 1