MostlyRquestions
MostlyRquestions

Reputation: 566

working with JSON with a bash script

I’m writing a bash script that does a few things. Right now it copies a few files into correct directories and runs a few commands. I need this bash script to edit a JSON file. Essentially this script would append a snippet of JSON to an existing JSON object that exists in a file.JSON. I cannot just append the data because the JSON snippet must be a part of an existing JSON object (should be added to the tracks array). So is this possible to do with a bash script? Should I just write another python or R script to handle this JSON Logic or is there a more elegant solution. Thanks for any help.

file.JSON looks like this...
    {
       "formatVersion" : 1,
       "tracks" : [
          {
             "key" : "Reference sequence",
             "chunkSize" : 20000,
             "urlTemplate" : "seq/{refseq_dirpath}/{refseq}-",
             "storeClass" : "JBrowse/Store/Sequence/StaticChunked",
             "type" : "SequenceTrack",
             "seqType" : "dna",
             "category" : "Reference sequence",
             "label" : "DNA"
          },
          {
             "type" : "FeatureTrack",
             "label" : "gff_track1",
             "trackType" : null,
             "key" : "gff_track1",
             "compress" : 0,
             "style" : {
                "className" : "feature"
             },
             "storeClass" : "JBrowse/Store/SeqFeature/NCList",
             "urlTemplate" : "tracks/gff_track1/{refseq}/trackData.json"
          },
          {
             "storeClass" : "JBrowse/Store/SeqFeature/NCList",
             "style" : {
                "className" : "feature"
             },
             "urlTemplate" : "tracks/ITAG2.4_gene_models.gff3/{refseq}/trackData.json",
             "key" : "ITAG2.4_gene_models.gff3",
             "compress" : 0,
             "trackType" : null,
             "label" : "ITAG242.4_gene_models.gff3",
             "type" : "FeatureTrack"
          },
          {
             "urlTemplate" : "g-231FRL.bam",
             "storeClass" : "JBrowse/Store/SeqFeature/BAM",
             "label" : "g-1FRL.bam",
             "type" : "JBrowse/View/Track/Alignments2",
             "key" : "g-1FRL.bam"
          }
       ]
    }

the JSON snippet looks like this ...

      {
         "urlTemplate": "AX2_filtered.vcf.gz",
         "label": "AX2_filtered.vcf.gz",
         "storeClass": "JBrowse/Store/SeqFeature/VCFTabix", 
         "type": "CanvasVariants"
      }

Upvotes: 0

Views: 552

Answers (1)

zwer
zwer

Reputation: 25799

Do yourself a favor and install jq, then it's as simple as:

jq -n 'input | .tracks += [inputs]' file.json snippet.json > out.json

Trying to modify a structured data (like JSON) is a fool's errand without a proper parser and jq really makes it easy.

However, if you prefer doing it through Python (although it would be an overkill for this kind of a task) it's pretty much as straight forward as with jq:

import json

with open("file.json", "r") as f, open("snippet.json", "r") as s, open("out.json", "w") as u:
    data = json.load(f)  # parse `file.json`
    data["tracks"].append(json.load(s))  # parse `snippet.json` and append it to `.tracks[]`
    json.dump(data, u, indent=4)  # encode the data back to JSON and write it to `out.json`

Upvotes: 2

Related Questions