我试图使用jq从JSON对象中添加和删除一个'key:value‘。我是新使用jq的,我不理解jq向我抛出的错误,所以任何帮助我朝着正确的方向前进都是非常感谢的。我的具体问题是我有一个JSON对象(下面),我希望能够从JSON对象中添加/删除“maxHeight”键/值。
一些命令,我尝试了我得到的错误…
jq 'recurse(.[]) |= del(.maxHeight)' new.json 不能在空(空)上迭代
jq 'recurse(.[]) |= {maxHeight}' new.json不能遍历字符串(“特性”)
jq 'recurse(.[]) |= .maxHeight' new.json 不能用字符串“样式”索引字符串
new.json文件看起来像这样..。
{
"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"
}发布于 2018-01-30 20:07:56
有两种方法:
以下说明了全球办法:
walk(if type == "object" and has("maxHeight") then del(.maxHeight) else . end)这实际上是通过更新具有指定键的任何对象来“编辑”输入。
如果您的jq没有walk/1,那么在调用它之前只需包含它的def (可以从https://raw.githubusercontent.com/stedolan/jq/master/src/builtin.jq获得)。
发布于 2020-03-17 13:23:21
使用jq-1.6,这将从输入中删除键.maxHeight (如果它以前不存在,甚至不会抱怨):
jq 'del(.maxHeight)' new.json发布于 2019-02-04 14:11:19
我也有过类似的问题,但我不想为此做很多代码,也不想花太多时间在上面。
我猜你已经解决了你的原因。但是对于我来说,下面的工作是为了一个不需要递归查找的值,即只在顶层。此外,我也不关心是否存在空/空值:
jq "if .maxHeight then .maxHeight = null else . end "https://stackoverflow.com/questions/48529016
复制相似问题