I'm using jq 1.6 on Windows 7 and want to add a dynamically generated array to a json file.
That array doesn't yet exist in this file.
I've got the following JSON structure (reduced for reasons of clarity):
{
"policies": {
"SearchBar": "separate",
"SearchEngines": {
"PreventInstalls": false
}
}
}
I'd like to generate an array based on dynamic values and finally create the following output:
{
"policies": {
"SearchBar": "separate",
"SearchEngines": {
"PreventInstalls": false,
"Remove": [
"Twitter",
"Wikipedia (en)"
]
}
}
}
The Remove array's content is stored in a (cmd) %variable%.
I found that the line
jq -n --arg items "%variable%" "{ Remove: $items | split(\",\") }"
produces the array I want:
{
"Remove": [
"Twitter",
"Wikipedia (en)"
]
}
What is the best way to insert this array into the original file?
Given the string input string Twitter,Wikipedia (en), you can use jq to update the JSON data:
<file jq --arg i 'Twitter,Wikipedia (en)' '.policies.SearchEngines += ({ Remove: $i | split(",") })'
{
"policies": {
"SearchBar": "separate",
"SearchEngines": {
"PreventInstalls": false,
"Remove": [
"Twitter",
"Wikipedia (en)"
]
}
}
}
Related
I am quite new with JQ library, I want to filter the json file using their name(eg. release-1),Then I want to return key value of the all commitId in the same object as the name.
My json file
{
"releases":[
{
"name":[
"release-1"
],
"artifacts":[
{
"name":"pkg-1",
"commitId":"523asdc3"
},
{
"name":"pkg-2",
"commitId":"523asdc3"
},
{
"name":"pkg-3",
"commitId":"523asdc3"
}
]
},
{
"name":[
"release-2"
],
"artifacts":[
{
"name":"pkg-3",
"commitId":"523asdc3"
},
{
"name":"pkg-4",
"commitId":"523asdc3"
},
{
"name":"pkg-5",
"commitId":"523asdc3"
}
]
}
]
}
Expected Output
523asdc3
523asdc3
523asdc3
.releases[] | select(.name | index("release-1")) | .artifacts[].commitId
Will show each commitId where name contains (index()) release-1.
Result:
"523asdc3"
"523asdc3"
"523asdc3"
JqPlay demo
.releases[]|select(.name|contains(["release-1"]))|.artifacts[].commitId
is another way.
.releases[]|select(.name[0] =="release-1")|.artifacts[].commitId
is yet another way.
Consider the below JSON:
[
{
"vd":[
{
"key":1
},
{
"key":2
}
]
}
]
Now I would like to filter based on the filter key==1. So the result should be:
[
{
"vd": [
{
"key": 1
}
]
}
]
Is there a way to achieve this using jq?
Traverse to the array in question .[].vd, and update |= with a map based on a select using your criteria .key == 1:
jq '.[].vd |= map(select(.key == 1))'
[
{
"vd": [
{
"key": 1
}
]
}
]
Demo
This is my sample input
Input
[
{
"label": "test1",
"value": 1,
"path": "data/testData/testDataLevel3/testDataLevel3_1/0/testDataLevel3_1_a2"
},
{
"label": "test2",
"value": 2,
"path": "data/testData/testDataLevel1/testDataLevel1_1"
}
]
This input needs to be converted like this using jq
Expected output:
{
"data": {
"testData": {
"testDataLevel1": { //object
"testDataLevel1_1": 2
},
"testDataLevel3": {
"testDataLevel3_1": [ //array
{
"testDataLevel3_1_a2": 1
}
]
}
}
}
}
The path will contain the array index as path, and sometimes the keys will be combined in the path as well
You need to convert each .path to a form setpath can understand. The rest is straightforward.
reduce .[] as {$path, $value} (null;
setpath($path / "/" | map(tonumber? // .); $value)
)
Online demo
I'm starting with a JSON file that has multiple objects
{
"name": "foo",
"url": "https://zombo.com"
}
{
"name": "bar",
"url": "https://acme.com"
}
and I'm trying to combine those into a single object with the name attribute as the key:
{
"widgets": {
"foo": {
"url": "https://zombo.com"
},
"bar": {
"url": "https://acme.com"
}
}
}
I've been banging my head against this for a while and I think I'm fairly close with the following query:
{ widgets: (reduce . as $item ({}; . + {($item.name): {url: $item.url}})) }
However, this results in multiple objects and I'm running out of ideas.
jqplay available here: https://jqplay.org/s/SI7XEMb5l9.
I'd do it like this:
jq -s '{ widgets: map( { (.name): {url} } ) | add }'
-s (--slurp) combines the objects to an array of objects
map( { (.name): { url } } ) reshapes each object
add concatenates the array elements into a single object
and finally, I build an object with { widgets: ... }.
Use reduce with inputs:
jq -n '{ widgets : (reduce inputs as $p ({}; . + ($p | { (.name) : { url } }))) }' file
If I have a json file as follows:
{
config: {
name: "test1"
}
}
How would I use jq to create an empty object named after "test1" in a new file?
i.e.
test1: {
}
First I suggest you make original file a proper JSON by adding quotes to keys:
{
"config": {
"name": "test1"
}
}
Now you can do it like this:
jq '{(.config.name):{}}' config.name
Output:
{
"test1": {}
}