【问题标题】:Manipulating a JSON file with jq使用 jq 操作 JSON 文件
【发布时间】:2018-01-30 16:43:43
【问题描述】:

我正在使用 jq 来操作 JSON 文件。我需要运行一个搜索数组的命令,找到一个具有等于“someData”的键的对象,然后删除该对象中的一个键,例如,从其中键的对象中删除一个“maxHeight”键等于“一些数据”。这是一个示例 JSON 文件,我想搜索 ‘tracks’ 数组并找到 label =“cucumber_ChineseLong_v2.gff3” 的对象,然后从该对象中删除 ‘maxHeight: “200px”’。这甚至可以使用 jq 在单个命令中运行吗?

更新:我想要的输出将是删除了“maxHeight”键值的原始 JSON 文件。

更新:我尝试过的一些命令..

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.json
  • Last 命令可以隔离正确的数组,现在我需要知道如何使用 jq 从对象中删除键。

JSON 文件

{
  "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
}

【问题讨论】:

  • 为什么不可能呢?到目前为止你尝试过什么?
  • 我是 jq 的新手,如果可以的话,你能给我举个例子吗?
  • 我对 jq 没有太多经验,但据我所知,我希望您的要求是可行的。这是一个很好的起点来熟悉它并找出它:stedolan.github.io/jq/tutorial
  • 是的,我从那里开始,这也有帮助,github.com/stedolan/jq/issues/323,但我仍在为这个命令而苦苦挣扎。

标签: json jq


【解决方案1】:

不幸的是,您没有遵循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 . end

【讨论】:

  • 对不起,我没有正确定义我想要的输出,我正在寻找输出相同的文件,除了我想从该特定数组中删除“maxHeight”变量。您上面的代码很棒,但只输出该数组。是否可以返回删除“maxHeight”的原始文件?谢谢!
  • 太棒了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
  • 2019-06-11
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
相关资源
最近更新 更多