使用jq从JSON对象中删除key:value [英] Remove a key:value from an JSON object using jq
问题描述
我正在尝试使用jq从JSON对象添加和删除"key:value".我是jq的新手,但我不了解jq会给我带来的错误,因此,非常有帮助我朝正确的方向发展.我的特定问题是我有一个JSON对象(如下),并且我希望能够从JSON对象添加/删除"maxHeight"键/值.
I’m trying to add and remove a 'key:value' from a JSON object using jq. I’m new using jq and I do not understand the errors that jq is throwing at me, so any help pushing me in the correct direction is greatly appreciated. My specific issue is I have a JSON object (below) and I want to be able to add/remove the "maxHeight" key/value from the JSON object.
一些我尝试过的错误提示命令...
Some commands I’ve tried with the errors I get…
jq 'recurse(.[]) |= del(.maxHeight)' new.json
无法遍历null(null)
Cannot iterate over null (null)
jq 'recurse(.[]) |= {maxHeight}' new.json
无法遍历字符串(功能")
Cannot iterate over string ("feature")
jq 'recurse(.[]) |= .maxHeight' new.json
无法索引字符串为样式"的字符串
Cannot index string with string "style"
new.json文件看起来像这样...
new.json file looks like this...
{
"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"
}
推荐答案
有两种方法:
-
有针对性的方法,如上一个问题的解答所示,在带有jq
忽略特定上下文的全局方法.
the global approach, that ignores the specific context.
以下说明了全局方法:
walk(if type == "object" and has("maxHeight") then del(.maxHeight) else . end)
这实际上是通过更新具有指定键的任何对象来编辑"输入.
This in effect "edits" the input by updating whichever objects have the specified key.
如果您的jq没有 walk/1
,只需添加其def(例如,可从
If your jq does not have walk/1
simply include its def (available e.g. from https://raw.githubusercontent.com/stedolan/jq/master/src/builtin.jq) before invoking it.
这篇关于使用jq从JSON对象中删除key:value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!