用jq操作JSON文件 [英] Manipulating a JSON file with jq

查看:143
本文介绍了用jq操作JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m using jq to manipulate a JSON file. I need to run a single command that searches an array, finds an object that has a key that equals "someData", and then delete a key that is in the object, for example, delete a "maxHeight" key from the object where key equals "someData". Here is an example JSON file, I would like to search the ‘tracks’ array and find the object where label ="cucumber_ChineseLong_v2.gff3" and then remove the ‘maxHeight: "200px" ‘ from that object. Is this even possible to run in a single command using jq?

更新:我想要的输出将是原始的JSON文件,其中已删除"maxHeight"键:值.

Update: My desired output would be the original JSON file with that 'maxHeight' key:value removed.

更新:我尝试了一些命令.

Update: A few commands I've tried..

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
    }
    

    推荐答案

    很遗憾,您没有遵循 http://stackoverflow.com上的指南/help/mcve ,因此很难从描述中得知期望的输出,但是从对象中删除键的最简单方法是使用del/1,如下所示:

    Unfortunately you have not followed the guidelines at http://stackoverflow.com/help/mcve so it is difficult to tell from your description what output you are expecting, but the simplest way to delete a key from an object is to use del/1 as in the following:

    .tracks
    | map(select(.label=="cucumber_ChineseLong_v2.gff3") 
          | del(.maxHeight))
    

    使用您的JSON作为输入,上述查询的结果相当长,因此我不会在此响应中包括它-"mcve"中的"m"代表"minimal".

    With your JSON as input, the result of the above query is rather long, so I won't include it in this response -- the "m" in "mcve" stands for "minimal".

    是否可以删除"maxHeight"以返回原始文件?

    Is it possible to return the original file with the 'maxHeight' removed?

    对于编辑"(与提取相反),诀窍是使用|=.可能有多种变体,例如:

    For "editing" (as opposed to extraction), the trick is to use |=. There are many variations possible, for example:

    .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
    

    这篇关于用jq操作JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆