Generate dynamic JSON in Go

What if we want to generate a JSON with dynamic key? How do we do that?

Photo by sebastiaan stam on Unsplash

Suppose we have GeneObject with three child objects are “Ncbi”, “Ensemble”, “GeneCards”. And we have Result Object contains GeneObject.

We want the GeneObject which will become the key, will have different value for each json. Like “AK45”, or “BQ18”. We can do it by using


in Go.

Running the code

will show the result like

"ncbi":{"symbol":"TP53","symbol_source":"HGNC","id":"7157","gene_name":"tumor protein p53","gene_synonyms":["P53","BCC7","LFS1","BMFS5","TRP53"],"biotype":"protein coding","contig":"17","start":7668402,"end":7687550,"reference_genome":"GRCh38","strand":"","description":"This gene encodes a tumor suppressor protein containing transcriptional activation, DNA binding, and oligomerization domains. The encoded protein responds to diverse cellular stresses to regulate expression of target genes, thereby inducing cell cycle arrest, apoptosis, senescence, DNA repair, or changes in metabolism. Mutations in this gene are associated with a variety of human cancers, including hereditary cancers such as Li-Fraumeni syndrome. Alternative splicing of this gene and the use of alternate promoters result in multiple transcript variants and isoforms. Additional isoforms have also been shown to result from the use of alternate translation initiation codons from identical transcript variants (PMIDs: 12032546, 20937277). [provided by RefSeq, Dec 2016]"},
"ncbi":{"symbol":"MAFIP","symbol_source":"HGNC","id":"727764","gene_name":"MAFF interacting protein (pseudogene)","gene_synonyms":["MIP","pp5644","TEKT4P4"],"biotype":"pseudo","contig":"14 Unlocalized Scaffold","start":53589,"end":115073,"reference_genome":"GRCh38","strand":"","description":"This gene was originally thought to be a protein coding gene. However, the encoded protein sequence is highly similar to the C-terminal sequence of the tektin-4 protein, and the transcript sequences of this gene are highly similar to the TEKT4 pseudogenes, which are found on chromosomes 4, 21 and Y, respectively. Therefore, this gene is thought to be another pseudogene of the TEKT4 gene (GeneID:150483). Multiple alternatively spliced transcript variants have been found for this gene. [provided by RefSeq, Mar 2012]"},

Happy coding guys ~~

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store