我正在使用 jq 来操作 JSON 文件。我需要运行一个搜索数组的命令,找到一个具有等于“someData”的键的对象,然后删除该对象中的一个键,例如,从该对象中删除一个“maxHeight”键 where key等于“someData”。这是一个示例 JSON 文件,我想搜索 'tracks' 数组并找到 label =“cucumber_ChineseLong_v2.gff3” 的对象,然后从该对象中删除 'maxHeight: “200px” '。这甚至可以使用 jq 在单个命令中运行吗?
更新:我想要的输出将是原始 JSON 文件,其中删除了“maxHeight”键:值。
更新:我尝试过的一些命令..
jq '. as $dot|[range(0;length)|select(.maxHeight == "200px")|$dot[.]]' trackList.json > new.json
jq '.tracks[] | select(.label == "cucumber_ChineseLong_v2.gff3")' trackList.json > test.json
Run Code Online (Sandbox Code Playgroud)
JSON 文件
{
"tracks": [
{
"storeClass": "JBrowse/Store/Sequence/StaticChunked",
"chunkSize": 20000,
"urlTemplate": "seq/{refseq_dirpath}/{refseq}-",
"label": "DNA",
"type": "SequenceTrack",
"category": "Reference sequence",
"seqType": "dna",
"key": "Reference sequence"
},
{
"style": {
"className": "feature",
"showLabels": false,
"arrowheadClass": null,
"featureCss": "padding:3px;"
},
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Highlight a gene"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( feature[2] ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
],
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Reference\",\"Missing\",\"Heterozygous\",\"NonReference\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"NonReference\"){div.style.backgroundColor=\"red\"}else if(feature[i]==\"Reference\"){div.style.backgroundColor=\"green\"}else if(feature[i]==\"Heterozygous\"){div.style.backgroundColor=\"orange\"}else if(feature[i]==\"Missing\"){div.style.backgroundColor=\"grey\"} }}}}} "
},
"key": "CG0001.gff",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/CG0001.gff/{refseq}/trackData.json",
"compress": 0,
"label": "CG0001.gff",
"type": "FeatureTrack"
},
{
"style": {
"className": "feature",
"showLabels": false,
"arrowheadClass": null,
"featureCss": "padding:3px;"
},
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Highlight a gene"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( feature[2] ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
],
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Reference\",\"Missing\",\"Heterozygous\",\"NonReference\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"NonReference\"){div.style.backgroundColor=\"red\"}else if(feature[i]==\"Reference\"){div.style.backgroundColor=\"green\"}else if(feature[i]==\"Heterozygous\"){div.style.backgroundColor=\"orange\"}else if(feature[i]==\"Missing\"){div.style.backgroundColor=\"grey\"} }}}}} "
},
"key": "CG0002.gff",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/CG0002.gff/{refseq}/trackData.json",
"compress": 0,
"label": "CG0002.gff",
"type": "FeatureTrack"
},
{
"style": {
"className": "feature",
"showLabels": false,
"arrowheadClass": null,
"featureCss": "padding:3px;"
},
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Highlight a gene"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( feature[2] ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
],
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Reference\",\"Missing\",\"Heterozygous\",\"NonReference\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"NonReference\"){div.style.backgroundColor=\"red\"}else if(feature[i]==\"Reference\"){div.style.backgroundColor=\"green\"}else if(feature[i]==\"Heterozygous\"){div.style.backgroundColor=\"orange\"}else if(feature[i]==\"Missing\"){div.style.backgroundColor=\"grey\"} }}}}} "
},
"key": "CG0003.gff",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/CG0003.gff/{refseq}/trackData.json",
"compress": 0,
"label": "CG0003.gff",
"type": "FeatureTrack"
},
{
"style": {
"className": "feature",
"showLabels": false,
"arrowheadClass": null,
"featureCss": "padding:3px;"
},
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Highlight a gene"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( feature[2] ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
],
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Reference\",\"Missing\",\"Heterozygous\",\"NonReference\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"NonReference\"){div.style.backgroundColor=\"red\"}else if(feature[i]==\"Reference\"){div.style.backgroundColor=\"green\"}else if(feature[i]==\"Heterozygous\"){div.style.backgroundColor=\"orange\"}else if(feature[i]==\"Missing\"){div.style.backgroundColor=\"grey\"} }}}}} "
},
"key": "CG0004.gff",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/CG0004.gff/{refseq}/trackData.json",
"compress": 0,
"label": "CG0004.gff",
"type": "FeatureTrack"
},
{
"style": {
"className": "feature",
"showLabels": false,
"arrowheadClass": null,
"featureCss": "padding:3px;"
},
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Highlight a gene"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( feature[2] ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
],
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Reference\",\"Missing\",\"Heterozygous\",\"NonReference\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"NonReference\"){div.style.backgroundColor=\"red\"}else if(feature[i]==\"Reference\"){div.style.backgroundColor=\"green\"}else if(feature[i]==\"Heterozygous\"){div.style.backgroundColor=\"orange\"}else if(feature[i]==\"Missing\"){div.style.backgroundColor=\"grey\"} }}}}} "
},
"key": "CG0005.gff",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/CG0005.gff/{refseq}/trackData.json",
"compress": 0,
"label": "CG0005.gff",
"type": "FeatureTrack"
},
{
"style": {
"className": "feature",
"showLabels": false,
"arrowheadClass": null,
"featureCss": "padding:3px;"
},
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Highlight a gene"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( feature[2] ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
],
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Reference\",\"Missing\",\"Heterozygous\",\"NonReference\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"NonReference\"){div.style.backgroundColor=\"red\"}else if(feature[i]==\"Reference\"){div.style.backgroundColor=\"green\"}else if(feature[i]==\"Heterozygous\"){div.style.backgroundColor=\"orange\"}else if(feature[i]==\"Missing\"){div.style.backgroundColor=\"grey\"} }}}}} "
},
"key": "CG0010.gff",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/CG0010.gff/{refseq}/trackData.json",
"compress": 0,
"label": "CG0010.gff",
"type": "FeatureTrack"
},
{
"style": {
"className": "feature",
"showLabels": false,
"color": "function(feature, variableName, glyphObject, track){if(feature.get(\"type\") === \"CDS\"){return \"#9CFBF5\";} else if(feature.get(\"type\") === \"exon\"){return \"#43A47F\";} else if(feature.get(\"type\") === \"intron\"){return \"#E8E8E8\";} else if(feature.get(\"type\") === \"five_prime_UTR\"){return \"#F192FE\";} else if(feature.get(\"type\") === \"three_prime_UTR\"){return \"#FEC892\";} else {return \"#FF0000\";}}",
"arrowheadClass": null,
"featureCss": "padding:3px;"
},
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Highlight a gene"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( feature[2] ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
],
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Reference\",\"Missing\",\"Heterozygous\",\"NonReference\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"NonReference\"){div.style.backgroundColor=\"red\"}else if(feature[i]==\"Reference\"){div.style.backgroundColor=\"green\"}else if(feature[i]==\"Heterozygous\"){div.style.backgroundColor=\"orange\"}else if(feature[i]==\"Missing\"){div.style.backgroundColor=\"grey\"} }}}}} "
},
"key": "cucumber_ChineseLong_v2.gff3",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/cucumber_ChineseLong_v2.gff3/{refseq}/trackData.json",
"compress": 0,
"label": "cucumber_ChineseLong_v2.gff3",
"type": "JBrowse/View/Track/CanvasFeatures"
},
{
"storeClass": "JBrowse/Store/SeqFeature/VCFTabix",
"urlTemplate": "Cucumber115_chronly.vcf.gz",
"maxHeight": "200px",
"type": "CanvasVariants",
"label": "Cucumber115_chronly.vcf",
"menuTemplate": [
{
"label": "View details"
},
{
"label": "Zoom"
},
{
"iconClass": "dijitIconFilter",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().selectSequence( div.f.data.end ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Select Sequence"
},
{
"iconClass": "dijitIconBookmark",
"content": "function(track,feature,div) { window.parent.angular.element(window.frameElement).scope().specificNote( div.f.data.end ) }",
"action": "contentDialog",
"title": "(feature{name})",
"label": "Create Note"
}
]
},
{
"style": {
"className": "feature",
"showLabels": false,
"arrowheadClass": null,
"featureCss": "padding:7px;"
},
"hooks": {
"modify": " function(track,feature,div){ var checkArr=[\"Gene\",\"Variant\",\"Primer\"];for(var i=0;i<feature.length;i++){for(var j=0;j<checkArr.length;j++){ if( i>3) { if( feature[i] === checkArr[j] ) { if(feature[i]==\"Gene\"){div.style.backgroundColor=\"pink\"}else if(feature[i]==\"Variant\"){div.style.backgroundColor=\"purple\"}else if(feature[i]==\"Primer\"){div.style.backgroundColor=\"blue\"} }}}}} "
},
"key": "Notes",
"storeClass": "JBrowse/Store/SeqFeature/NCList",
"trackType": null,
"maxHeight": "200px",
"urlTemplate": "tracks/Notes/{refseq}/trackData.json",
"compress": 0,
"type": "FeatureTrack",
"label": "Notes"
}
],
"formatVersion": 1
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,你还没有在遵循的准则http://stackoverflow.com/help/mcve因此很难从你的描述,你期待什么样的输出,但最简单的方法告诉给删除对象的关键是使用del/1在下列:
.tracks
| map(select(.label=="cucumber_ChineseLong_v2.gff3")
| del(.maxHeight))
Run Code Online (Sandbox Code Playgroud)
以您的 JSON 作为输入,上述查询的结果相当长,因此我不会将其包含在此响应中——“mcve”中的“m”代表“最小”。
是否可以返回删除“maxHeight”的原始文件?
对于“编辑”(而不是提取),诀窍是使用|=. 可能有许多变体,例如:
.tracks |=
map(if .label=="cucumber_ChineseLong_v2.gff3"
then del(.maxHeight)
else .
end)
Run Code Online (Sandbox Code Playgroud)
有些人更喜欢单线:
.tracks[] |= if .label=="cucumber_ChineseLong_v2.gff3" then del(.maxHeight) else . end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2848 次 |
| 最近记录: |