我正在编写一个bash脚本,它可以做一些事情。现在,它将一些文件复制到正确的目录中,并运行一些命令。我需要这个bash脚本来编辑JSON文件。我不能仅仅追加数据,因为JSON片段必须是现有JSON对象的一部分(应该添加到file.JSON数组中)。那么使用bash脚本可以做到这一点吗?我应该只编写另一个python或R脚本来处理这个JSON逻辑,还是有一个更好的解决方案。谢谢你的帮助。
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"
}发布于 2017-07-12 02:16:37
帮你自己一个忙,安装jq,然后就像这样简单:
jq -n 'input | .tracks += [inputs]' file.json snippet.json > out.json如果没有适当的解析器,尝试修改结构化数据(如JSON)是一件愚蠢的事情,而jq确实让这件事变得很容易。
但是,如果您更喜欢使用Python语言(尽管对于这类任务来说有点夸大其词),那么它与使用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`https://stackoverflow.com/questions/45040567
复制相似问题