Json file editing through jq - json

I have json
{
"file1": [{
"username": "myname",
"groupname": "mypassword",
"environment": [{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": "2"
}]
}]
}
and want to change the value of DEBUG to 5.
Tried with below command
jq .file1[0].environment sandeep.json |jq '(.[] |select(.name ==
"DEBUG") | .value) |= "5"'
this will return me specific portion of json like
[
{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": "5"
}
]
but I want to see full json with changed value
{
"file1": [{
"username": "myname",
"groupname": "mypassword",
"environment": [{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": "5"
}]
}]
}
Please suggest me

It should be:
jq '(.file1[].environment[]|select(.name=="DEBUG").value) |= 5' file.json
Output:
{
"file1": [
{
"username": "myname",
"groupname": "mypassword",
"environment": [
{
"name": "UMASK",
"value": "022"
},
{
"name": "DEBUG",
"value": 5
}
]
}
]
}

Related

I want the value of "title" that should be "pipeline-pr-test" and value of "pr" that should be "2" from below code by using shell and jq?

[
{
"name": "pr",
"value": "2"
},
{
"name": "title",
"value": "pipeline-pr-test"
},
{
"name": "headhhf",
"value": "60dd35gt6"
},
{
"name": "base",
"value": "air"
},
{
"name": "basha",
"value": "7yhfr7e85a0cc"
}
]
Below query help me to get an output:
jq -r 'INDEX(.name)["title", "pr"] | "\(.name) = \(.value)"' test.json

JQ: Add object to nested json with the same key names

I having trouble of getting my json append with a new object into the config -> list -> key(vehicles) -> Rows. But then only for vehicles.
Im trying it with JQ: cat file.json | jq '.config.list[].rows[] += {"data":[{"key":"fort","value":"K"},{"key":"seat","value":"leon"}],"default":false}' But with this it is replacing and not appending because of the same names ?
Object that needs to
{
"data": [
{
"key": "bike",
"value": "yyy"
},
{
"key": "car",
"value": "xxx"
}
],
"default": false
}
Source Json:
{
"id": "1234",
"name": "CatList",
"config": {
"list": [
{
"key": "vehicles",
"rows": [
{
"data": [
{
"key": "bike",
"value": "yyy"
},
{
"key": "car",
"value": "xxx"
}
],
"default": false
}
]
},
{
"key": "boots",
"rows": []
}
],
"data": [
{
"key": "GROUPS",
"value": "false"
}
]
}
}
Wanted result:
{
"id": "1234",
"name": "CatList",
"config": {
"list": [
{
"key": "vehicles",
"rows": [
{
"data": [
{
"key": "bike",
"value": "yyy"
},
{
"key": "car",
"value": "xxx"
}
],
"default": false
},
{
"data": [ <-----
{ <-----
"key": "bike", <-----
"value": "yyy" <-----
}, <-----
{ <-----
"key": "car", <-----
"value": "xxx" <-----
} <-----
], <-----
"default": false <-----
}
]
},
{
"key": "boots",
"rows": []
}
],
"data": [
{
"key": "GROUPS",
"value": "false"
}
]
}
}
You were close
jq '.config.list[.config.list|map(.key=="vehicles")|index(true)].rows += [{"data":[{"key":"fot","value":"K"},{"key":"seat","value":"leon"}],"default":false}]'
see https://stackoverflow.com/a/42248841/2235381

Select value from array json using jq

I have the following json file which this value
{
"data": [
{
"id": 60573936,
"timeCreated": 1615458564202,
"timeUpdated": 1615458564202,
"name": "name1",
"desiredStatus": "UNMANAGED",
"defaultStatus": "STARTED",
"targets": [
{
"id": 2520949,
"lastReportedStatus": "STOPPED"
}, {
"id": 702881,
"lastReportedStatus": "STARTED"
}
]
}, {
"id": 60574370,
"timeCreated": 1615458812565,
"timeUpdated": 1615458812565,
"name": "name2",
"desiredStatus": "UNMANAGED",
"defaultStatus": "STARTED",
"targets": [
{
"id": 2520949,
"lastReportedStatus": "STARTED"
}, {
"id": 702881,
"lastReportedStatus": "STOPPED"
}
]
}, {
"id": 60574329,
"timeCreated": 1615458775053,
"timeUpdated": 1615458775053,
"name": "name3",
"desiredStatus": "UNMANAGED",
"defaultStatus": "STARTED",
"targets": [
{
"id": 2520949,
"lastReportedStatus": "STOPPED"
}, {
"id": 702881,
"lastReportedStatus": "STARTED"
}
]
}
]
}
I would like to extract the "name" value only if "id"="2520949" and "lastReportedStatus"="STOPPED" using jq. In my example I would like to get "name1" and "name3". I tried using select feature but I'm not able to satisfy the "and" condition between "id" and "lastReportedStatus" key. What is the correct code with jq ?
jq '.data[]
| select(.targets[] | .lastReportedStatus=="STOPPED" and .id==2520949)
| .name' file.json

unable to extract value using ._find

I have the following node.js code where I want to extract the value "abc" chosen by user as a name. I get result as undefined when I run this code:
let input= [
{
"param": [
{
"id": "name",
"choice": [
{
"label": "abc",
"value": "abc",
"valueId": "abc"
}
]
},
{
"id": "alias",
"choice": [
{
"label": "dsf",
"value": "dsf",
"valueId": "dsf"
}
]
},
{
"id": "description",
"choice": [
{
"label": "",
"value": "",
"valueId": ""
}
]
},
{
"id": "Key",
"choice": [
{
"label": "K",
"value": "K",
"valueId": "K"
}
]
},
{
"id": "tagKey",
"choice": [
{
"label": "",
"value": "",
"valueId": ""
}
]
},
{
"id": "tagValue",
"choice": [
{
"label": "",
"value": "",
"valueId": ""
}
]
},
{
"id": "multiquantity",
"choice": [
{
"label": "1",
"valueId": "1",
"value": "1"
}
]
}
],
"old": [],
"current": null
}
]
let result = (_.find(input.param, {id: "name"})).choice[0].valueId;
console.log("value"+result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Seems that the logic is not able to extract the correct value. I need the output as abc. please help

json - query with conditions

I have following json:
{
"id": "1",
"name": "profile1",
"userId": "0",
"groupId": "3",
"attributes": [
{
"id": "104",
"name": "Enable",
"value": "1"
},
{
"id": "105",
"name": "TargetNode",
"value": "system1"
},
{
"id": "106",
"name": "Timeout",
"value": "30"
}
],
"xconns": [
{
"id": "1",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "1",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "1",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
}
]
}
{
"id": "2",
"name": "profile2",
"userId": "7",
"groupId": "0",
"attributes": [
{
"id": "104",
"name": "Enable",
"value": "1"
},
{
"id": "105",
"name": "TargetNode",
"value": "system2"
},
{
"id": "106",
"name": "Timeout",
"value": "30"
}
],
"xconns": [
{
"id": "2",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "2",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
},
{
"id": "2",
"locked": false,
"attributeList": [
{
"id": "101",
"name": "Lgrp",
"value": "1"
},
{
"id": "102",
"name": "IsRem",
"value": "1"
},
{
"id": "103",
"name": "Media",
"value": "1"
}
]
}
]
}
I can filter following:
$ jq -r 'select([.attributes[] | .name == "TargetNode" ] | any ) | [{userId, groupId, id, name}] | .[] | if (.userId == "0") then del(.userId) else . end | if (.groupId == "0") then del(.groupId) else . end | to_entries | map("\(.key | ascii_upcase):\(.value)") | #tsv' file.json
GROUPID:3 ID:1 NAME:profile1
USERID:7 ID:2 NAME:profile2
I need to add also value of TargetNode:
GROUPID:3 ID:1 NAME:profile1 TARGETNODE:system1
USERID:7 ID:2 NAME:profile2 TARGETNODE:system2
is there a way to include it in
[{userId, groupId, id, name, TargetNode}]
to get the value of TargetNode and not null?
GROUPID:3 ID:1 NAME:profile1 TARGETNODE:null
USERID:7 ID:2 NAME:profile2 TARGETNODE:null
Update:
the solution provided by RomanPerekhrest is nearly ok, but there is one issue because the json file in real is much bigger, there are more attrobutes in "main secttion", for example:
{
"id": "1",
"name": "profile1",
"userId": "0",
"groupId": "3",
"attrib101": "A",
"attrib102": "B",
"attributes": [
...
...
it is cousing that RomanPerekhrest's jq filter returns too much...
how to rid of them too?
ID:1 NAME:profile1 GROUPID:3 ATTRIB101:A ATTRIB102:B TARGETNODE:system1
ID:2 NAME:profile2 USERID:7 ATTRIB101:C ATTRIB102:D TARGETNODE:system2
jq solution:
jq -r '.attributes |= map(select(.name == "TargetNode"))
| if (.attributes | length != 0) then .targetNode = .attributes[0].value else . end
| if (.userId == "0") then del(.userId) else . end
| if (.groupId == "0") then del(.groupId) else . end
| del(.attributes, .xconns) | to_entries
| map("\(.key | ascii_upcase):\(.value)") | #tsv' file.json
If an object with "name": "TargetNode" pair not exists - TARGETNODE won't be added into resulting structure
The output:
ID:1 NAME:profile1 GROUPID:3 TARGETNODE:system1
ID:2 NAME:profile2 USERID:7 TARGETNODE:system2