首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jq从JSON对象中移除键:值

使用jq从JSON对象中移除键:值
EN

Stack Overflow用户
提问于 2018-01-30 19:21:25
回答 5查看 22.3K关注 0票数 15

我试图使用jq从JSON对象中添加和删除一个'key:value‘。我是新使用jq的,我不理解jq向我抛出的错误,所以任何帮助我朝着正确的方向前进都是非常感谢的。我的具体问题是我有一个JSON对象(下面),我希望能够从JSON对象中添加/删除“maxHeight”键/值。

一些命令,我尝试了我得到的错误…

代码语言:javascript
复制
jq 'recurse(.[]) |= del(.maxHeight)' new.json   

不能在空(空)上迭代

代码语言:javascript
复制
 jq 'recurse(.[]) |= {maxHeight}' new.json

不能遍历字符串(“特性”)

代码语言:javascript
复制
jq 'recurse(.[]) |= .maxHeight' new.json 

不能用字符串“样式”索引字符串

new.json文件看起来像这样..。

代码语言:javascript
复制
{
  "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"
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-01-30 20:07:56

有两种方法:

以下说明了全球办法:

代码语言:javascript
复制
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获得)。

票数 6
EN

Stack Overflow用户

发布于 2020-03-17 13:23:21

使用jq-1.6,这将从输入中删除键.maxHeight (如果它以前不存在,甚至不会抱怨):

代码语言:javascript
复制
jq 'del(.maxHeight)' new.json
票数 24
EN

Stack Overflow用户

发布于 2019-02-04 14:11:19

我也有过类似的问题,但我不想为此做很多代码,也不想花太多时间在上面。

我猜你已经解决了你的原因。但是对于我来说,下面的工作是为了一个不需要递归查找的值,即只在顶层。此外,我也不关心是否存在空/空值:

代码语言:javascript
复制
jq "if .maxHeight then .maxHeight = null else . end "
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48529016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档