gpt4 book ai didi

json - 删除 key :value from an JSON object using jq

转载 作者:行者123 更新时间:2023-12-05 00:48:56 26 4
gpt4 key购买 nike

我正在尝试使用jq从JSON对象添加和删除“key:value”。我是jq的新手,但我不了解jq会给我带来的错误,因此,非常感谢向我推向正确方向的任何帮助。我的特定问题是我有一个JSON对象(如下),并且我希望能够从JSON对象添加/删除“maxHeight”键/值。

我尝试过的一些错误提示命令...

jq 'recurse(.[]) |= del(.maxHeight)' new.json   

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文件看起来像这样...
{
"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"
}

最佳答案

有两种方法:

  • 目标方法,在Manipulating a JSON file with jq上一个问题的答案中得到了说明
  • 全局方法,它忽略了特定的上下文。

  • 下面说明了全局方法:
    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获得)即可。

    关于json - 删除 key :value from an JSON object using jq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529016/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com