I'm trying to extract value of a branch from the below json/test.json file using jq
{
"pipeline": {
"name": "test",
"roleArn": "arn:aws:iam::1234:role/service-role/AWSCodePipelineServiceRole-us-west-2-test",
"artifactStore": {
"type": "S3"
},
"stages": [{
"name": "Source",
"actions": [{
"name": "Source",
"actionTypeId": {
"category": "Source",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "experiment"
}
}]
}],
"version": 1
}
}
Below is jq command I'm using jq -r '.pipeline.stages.actions.configuration.Branch' test.jsonwhich returns jq: error (at test.json:76): Cannot index array with string "actions". I'm I missing something here
When you're doing queries, and something doesn't work at first, then try something simple and keep adding to it. So in your case, start with
$ jq -r '.pipelines' test.json
null
Aha, it's "pipeline" not "pipelines", so go from there:
$ jq -r '.pipeline' test.json
{
"name": "test",
"roleArn": "arn:aws:iam::1234:role/service-role/AWSCodePipelineServiceRole-us-west-2-test",
"artifactStore": {
"type": "S3"
},
"stages": [
{
"name": "Source",
"actions": [
{
"name": "Source",
"actionTypeId": {
"category": "Source",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "experiment"
}
}
]
}
],
"version": 1
}
So that works. Now we want to get to "stages" so we do this
$ jq -r '.pipeline.stages' test.json
[
{
"name": "Source",
"actions": [
{
"name": "Source",
"actionTypeId": {
"category": "Source",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "experiment"
}
}
]
}
]
Note that "stages" gives you an array, not just a hash, so you have to refer to [0], the zeroth element.
$ jq -r '.pipeline.stages[0]' test.json
{
"name": "Source",
"actions": [
{
"name": "Source",
"actionTypeId": {
"category": "Source",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "experiment"
}
}
]
}
Now you can get to "actions"
$ jq -r '.pipeline.stages[0].actions' test.json
[
{
"name": "Source",
"actionTypeId": {
"category": "Source",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "experiment"
}
}
]
and then the zeroth one
$ jq -r '.pipeline.stages[0].actions[0]' test.json
{
"name": "Source",
"actionTypeId": {
"category": "Source",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "experiment"
}
}
and finally to configuration and Branch
$ jq -r '.pipeline.stages[0].actions[0].configuration.Branch' test.json
experiment
Your json key address should be .pipeline.stages[0].actions[0].configuration.Branch so that would make your command look like this:
jq -r '.pipeline.stages[0].actions[0].configuration.Branch' test.json
That's because both stages and actions are arrays with both only a single item in them.
Related
I have a problem with passing variables into jq. Here is example file:
[
{
"version": 24,
"file": "branding/24"
}
]
and here is my script:
jq --arg value "25" '. += [{"version": $value|tonumber, "file": "branding/$value|tonumber"}]' versions.json >tmp.json && mv tmp.json versions.json
The result is:
[
{
"version": 24,
"file": "branding/24"
},
{
"version": 25,
"file": "branding/$value|tonumber"
}
]
Expected result:
[
{
"version": 24,
"file": "branding/24"
},
{
"version": 25,
"file": "branding/25"
}
]
You need to use interpolation to have the filter evaluated.
{"version": $value|tonumber, "file": "branding/\($value|tonumber)"}
I am using aws ecs query to get list of properties being used by the current running task.
command -
cft = "aws ecs describe-tasks --cluster arn:aws:ecs:us-west-2:4984314772:cluster/secrets --tasks arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b
I am storing this in an output variable
output= $( eval $cft)
Output:
"tasks": [
{
"attachments": [
{
"id": "da8a1312-8278-46d5-8e3b-6b6a1d96f820",
"type": "ElasticNetworkInterface",
"status": "ATTACHED",
"details": [
{
"name": "subnetId",
"value": "subnet-0a151f2eb959ad4"
},
{
"name": "networkInterfaceId",
"value": "eni-081948e3666253f"
},
{
"name": "macAddress",
"value": "02:2a:9i:5c:4a:77"
},
{
"name": "privateDnsName",
"value": "ip-172-56-17-177.us-west-2.compute.internal"
},
{
"name": "privateIPv4Address",
"value": "172.56.17.177"
}
]
}
],
"availabilityZone": "us-west-2a",
"clusterArn": "arn:aws:ecs:us-west-2:4984314772:cluster/secrets",
"containers": [
{
"taskArn": "arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b",
"name": "nginx",
"image": "nginx",
"lastStatus": "PENDING",
"networkInterfaces": [
{
"attachmentId": "da8a1312-8278-46d5-6b6a1d96f820",
"privateIpv4Address": "172.31.17.176"
}
],
"healthStatus": "UNKNOWN",
"cpu": "0"
}
],
"cpu": "256",
"createdAt": "2020-12-10T18:00:16.320000+05:30",
"desiredStatus": "RUNNING",
"group": "family:nginx",
"healthStatus": "UNKNOWN",
"lastStatus": "PENDING",
"launchType": "FARGATE",
"memory": "512",
"overrides": {
"containerOverrides": [
{
"name": "nginx"
}
],
"inferenceAcceleratorOverrides": []
},
"platformVersion": "1.4.0",
"tags": [],
"taskArn": "arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b",
"taskDefinitionArn": "arn:aws:ecs:us-west-2:4984314772:task-definition/nginx:17",
"version": 2
}
],
"failures": []
}
now if do an echo of $output.tasks[0].containers[0] nothing happens it prints the entire thing again, i want to store the result in output variable and refer different parameter like we do in json format.
You will need to use a json parser such as jq and so:
eval $cft | jq '.tasks[].containers[]'
To avoid using eval you could simple pipe the aws command into jq and so:
aws ecs describe-tasks --cluster arn:aws:ecs:us-west-2:4984314772:cluster/secrets --tasks arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b | jq '.tasks[].containers[]'
or:
cft=$(aws ecs describe-tasks --cluster arn:aws:ecs:us-west-2:4984314772:cluster/secrets --tasks arn:aws:ecs:us-west-2:4984314772:task/secrets/86855757eec4487f9d4475a1f7c4cb0b | jq '.tasks[].containers[]')
echo $cft | jq '.tasks[].containers[]'
I know this one should be easy but it has me stumped. I am looking to take the following json example:
[{
"Name": "Test1",
"Version": "5.0.1",
"source": "source"
},
{
"Name": "Test2",
"Version": "2.0.11",
"source": "source"
},
{
"Name": "Test3",
"Version": "2.1.2",
"source": "source"
}]
and convert it to:
{
"packages": [
{
"Name": "Test1",
"Version": "5.0.1",
"source": "source"
},
{
"Name": "Test2",
"Version": "2.0.11",
"source": "source"
},
{
"Name": "Test3",
"Version": "2.1.2",
"source": "source"
}
]
}
I've tried numerous different ways, the closest I got is using something similar to: jq '.packages += [input]'
Basically it's just moving the original JSON to be nested. Any help would be appreciated.
just do this
jq '{ packages : . }' input.json
Even things you think of as literals are really filters in jq. The filter you need in this case is simply {packages: .}:
$ echo '[{}, {}]' | jq '{packages: .}'
{
"packages": [
{},
{}
]
}
I am trying to remove the duplicates from the following json by id
Here is the json:
{
"Result": [
{
"name": "validation-of-art",
"id": "12",
"status": "passed",
"duration": 4740302
},
{
"name": "validation-of-art",
"id": "12",
"status": "passed",
"duration": 272320
},
{
"name": "validation-of-art",
"id": "13",
"status": "passed",
"duration": 272320
}
]
}
Here is what i have tried with:
jq -r 'unique_by(.Result.name)'
and also with jq 'unique_by(.Result[].name)'
I am getting an error - Cannot index array with string "Result"
Any help would be appreciated.
Here is an example which eliminates all but one of the .Result objects using unique_by(.name)
$ jq -M '.Result |= unique_by(.name)' data.json
{
"Result": [
{
"name": "validation-of-art",
"id": "12",
"status": "passed",
"duration": 4740302
}
]
}
If this isn't quite what you want you can generalize this easily. E.g. to keep one object for each unique {name,id} you could use
$ jq -M '.Result |= unique_by({name, id})' data.json
{
"Result": [
{
"name": "validation-of-art",
"id": "12",
"status": "passed",
"duration": 4740302
},
{
"name": "validation-of-art",
"id": "13",
"status": "passed",
"duration": 272320
}
]
}
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
}
]
}
]
}