我使用jq来操作JSON文件。我需要运行一个命令来搜索数组,找到一个具有键等于“someData”的对象,然后删除对象中的一个键,例如,从键等于“someData”的对象中删除一个“maxHeight”键。这里是一个示例JSON文件,我想搜索‘tracks’数组并找到标签=“cucumber_ChineseLong_v2.gff3”的对象,然后从该对象中删除‘maxHeight:“”200 an“。这甚至可以使用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.jsonJSON文件
{
"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
}发布于 2018-01-30 19:19:02
不幸的是,您没有遵循http://stackoverflow.com/help/mcve的指导方针,因此很难从您的描述中知道您期望的输出,但是从对象中删除密钥的最简单方法是使用del/1,如下所示:
.tracks
| map(select(.label=="cucumber_ChineseLong_v2.gff3")
| del(.maxHeight))以您的JSON作为输入,上述查询的结果相当长,因此我不会将它包括在此响应中-- "mcve“中的"m”表示“极小”。
补充问答
是否可以在删除“maxHeight”后返回原始文件?
对于“编辑”(与提取相反),诀窍是使用|=。有许多可能的变化,例如:
.tracks |=
map(if .label=="cucumber_ChineseLong_v2.gff3"
then del(.maxHeight)
else .
end)有些人更喜欢一条线:
.tracks[] |= if .label=="cucumber_ChineseLong_v2.gff3" then del(.maxHeight) else . endhttps://stackoverflow.com/questions/48526476
复制相似问题