Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a key:value from an JSON object using jq

Tags:

json

jq

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   

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 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"
}
like image 948
Developing Avatar asked Jan 30 '18 19:01

Developing


People also ask

How do you remove a key value pair from a JSON object?

To remove JSON object key and value with JavaScript, we use the delete operator.

How do you remove a key value pair from a JSON array?

Remove Key Value Pair from JSON: In order to remove an attribute from json you have to use JS delete method. This will delete the json attribute with the specific key. Likewise you can remove the occurrence of particular attribute from json array.

How do I remove a property from a JSON object?

Using the Delete operator The Delete operator is the easiest way to delete the object property. if we want to delete multiple properties which are in the object, we need to use this operator multiple times. In the following example, we have created a JSON Object with keys and values.

How do I remove an item from a JSON file?

To remove JSON element, use the delete keyword in JavaScript.


2 Answers

Using jq-1.6, this deletes the key .maxHeight from the input (it doesn't even complain if it didn't exist before):

jq 'del(.maxHeight)' new.json
like image 122
exic Avatar answered Sep 19 '22 02:09

exic


For very large JSON documents, it may be preferable to use jq's "streaming parser" for this kind of problem, at least if the editing operations greatly reduce the size of the document. At any rate, here is a solution that uses the --stream option:

jq --stream 'select(length == 2 and .[0][-1] == "maxHeight" | not)' new.json |
 jq -n 'fromstream(inputs)'

Note that the -n option must be used in the second call to jq.

like image 32
peak Avatar answered Sep 22 '22 02:09

peak