I have these data in my json file:
{
"software": [{
"softwareId": "61426b91-0403-4e72-8bc3-bee6e4aabf00",
"softwareURL": "https://mydns/download/test.json",
"softwareName": "file1",
"softwareVersion": "1.0.0",
"softwareType": "FILE",
"signature": "yTzilsyq+0qVuioDD8cNT8zAtKt+qGTJ9aQ=="
}, {
"softwareId": "716a259b-c90b-47c8-856a-25e6bca61bc8",
"softwareURL": "https://mydns/download/test1.json",
"softwareName": "file2",
"softwareVersion": "2.0.0",
"softwareType": "FILE",
"signature": "vjmTQXgCxsw/q7Wz3a4g6N5Ht+Bhumo1acw=="
}, {
"softwareId": "c72644d5-6024-4783-a520-20bf39c8ea18",
"softwareURL": "https://mydns/download/test2.json",
"softwareName": "folder",
"softwareVersion": "1.0.0",
"softwareType": "FOLDER",
"signature": "hb35vQdVuhL7pRvRmV1OdGXYFLcpC4UKn0w=="
}
]
}
I want delete the json structure {} where "softwareType": "FILE" and "softwareName": "file2".
This should be the result:
{
"software": [{
"softwareId": "61426b91-0403-4e72-8bc3-bee6e4aabf00",
"softwareURL": "https://mydns/download/test.json",
"softwareName": "file1",
"softwareVersion": "1.0.0",
"softwareType": "FILE",
"signature": "yTzilsyq+0qVuioDD8cNT8zAtKt+qGTJ9aQ=="
}, {
"softwareId": "c72644d5-6024-4783-a520-20bf39c8ea18",
"softwareURL": "https://mydns/download/test2.json",
"softwareName": "folder",
"softwareVersion": "1.0.0",
"softwareType": "FOLDER",
"signature": "hb35vQdVuhL7pRvRmV1OdGXYFLcpC4UKn0w=="
}
]
}
Could you help me to do that with a sed command or like this?
Thanks in advance.
Theres an easy way to do this using jq, and it has been answered before: Delete json array elements based on values of subarray
Using jq:
jq '.software[] | select(.softwareType!="FILE" or .softwareName!="file2")' file.json
Use select to find where the softwareType is not "FILE" or softwareName is not "file2"
Related
Info
I have a terraform state file (json) with some deprecated attributes.
I would like to remove theses deprecated attributes.
I try to use jq and select() && del() but did not succeed to get back my full json without the deprecated attribue timeouts.
Problem
How to get my full json without the attribute timeouts for only one type of resources google_dns_record_set.
Data
{
"version": 4,
"terraform_version": "1.0.6",
"serial": 635,
"lineage": "6a9c2392-fdae-2b54-adcc-7366f262ffa4",
"outputs": {"test":"test1"},
"resources": [
{
"module": "module.resources",
"mode": "data",
"type": "google_client_config"
},
{
"module": "module.xxx.module.module1[\"cluster\"]",
"mode": "managed",
"type": "google_dns_record_set",
"name": "public_ip_ic_dns",
"provider": "module.xxx.provider[\"registry.terraform.io/hashicorp/google\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "projects/xxx-xxx/managedZones/xxx--public/rrsets/*.net1.cluster.xxx--public.net.com./A",
"managed_zone": "xxx--public",
"name": "*.net1.cluster.xxx--public.net.com.",
"project": "xxx-xxx",
"rrdatas": [
"11.22.33.44"
],
"timeouts": null,
"ttl": 300,
"type": "A"
},
"sensitive_attributes": [],
"private": "xxx",
"dependencies": [
"xxx"
]
}
]
}
]
}
Command
jq -r '.resources[] | select(.type=="google_dns_record_set").instances[].attributes | del(.timeouts)' data.json
Pull the del command up front to include the whole selection as its own filter
del(.resources[] | select(.type=="google_dns_record_set").instances[].attributes.timeouts)
Demo
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.
I have the list of docker images in json, for example:
{
"name": "chart1",
"version": "1.1.0",
"appVersion": "1.1.0",
"dependencies": [
{
"name": "name1",
"version": "10000.1.wew2133"
},
{
"name": "name2",
"version": "10001.1.wew2133"
}
]
}
I need to convert this to list:
name1:10000.1.wew2133
name2:10001.1.wew2133
How can I do this?
Use join(":"):
jq --raw-output '.dependencies[] | join(":")'
name1:10000.1.wew2133
name2:10001.1.wew2133
JQPlay demo
I'm working on finding a solution where I need to parse a json file (input.json) and create json file as output (output.json) after removing few lines.
Only few version will have both value (ubuntu, centos), most of the version will have either ubuntu or centos.
I tried the following command;
jq '.[]' input.json
it gives the complete list But don't know how to get values under ubuntu and centos. any guidance will be beneficial.
input.json
[
{
"Version": "1.0",
"ubuntu": {
"ver": "1.0.0",
"filename": "1_0_0.log",
"sourceUrl": "https://example.com/log/1_0_0.log"
}
},
{
"Version": "1.1",
"ubuntu": {
"ver": "1.0.1",
"filename": "1_0_1.log",
"sourceUrl": "https://example.com/log/1_0_1.log"
}
},
{
"Version": "1.4",
"ubuntu": {
"ver": "1.0.4",
"filename": "1_0_4.log",
"sourceUrl": "https://example.com/log/1_0_4.log"
},
"centos": {
"ver": "1.0.4",
"filename": "1_0_4.pdf",
"sourceUrl": "https://example.com/log/1_0_4.pdf"
}
}
]
output.json
[
{
"Version": "1.0",
"ubuntu": {
"ver": "1.0.0",
"filename": "1_0_0.log"
}
},
{
"Version": "1.1",
"ubuntu": {
"ver": "1.0.1",
"filename": "1_0_1.log"
}
},
{
"Version": "1.4",
"ubuntu": {
"ver": "1.0.4",
"filename": "1_0_4.log"
},
"centos": {
"ver": "1.0.14",
"filename": "1_0_4.pdf"
}
}
]
You can use the following JQ command to remove each sourceUrl;
jq 'del(.. | .sourceUrl?)' input.json
del() docs
Output:
[
{
"Version": "1.0",
"ubuntu": {
"version": "1.0.0",
"filename": "1_0_0.log"
}
},
{
"Version": "1.1",
"ubuntu": {
"version": "1.0.1",
"filename": "1_0_1.log"
}
},
{
"Version": "1.4",
"ubuntu": {
"version": "1.0.4",
"filename": "1_0_4.log"
},
"centos": {
"version": "1.0.4",
"filename": "1_0_4.pdf"
}
}
]
Try it online!
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": [
{},
{}
]
}