简体   繁体   中英

Extract key:value from JSON - Groovy

I have a complicated nested JSON from which I extract a LazyMap called "barcodes"

[bc0385:[Sample_ID:Tx0001B_ABCEFG, Gene(s):A,B,C,E,F,G, Primer_Set:research_ext_ABCE, Number of Gene(s):6, Class:1, LAA_minLength:[--minLength:2500, --ignoreEnds:60, --maxReads:12000, --maxPhasingReads:500, --maxClusteringReads:500]], bc0430:[Sample_ID:Tx0001C_ABCEFG, Gene(s):A,B,C,E,F,G, Primer_Set:research_ext_ABCE, Number of Gene(s):6, Class:1, LAA_minLength:[--minLength:2500, --ignoreEnds:60, --maxReads:12000, --maxPhasingReads:500, --maxClusteringReads:500]],...

What I am looking for is a neat map of {Barcode:LAA_minLength} like below:

{
  bc0385 : {--minLength:2500, --ignoreEnds:60, --maxReads:12000, --maxPhasingReads:500, --maxClusteringReads:500},
  bc0430 : {--minLength:2500, --ignoreEnds:60, --maxReads:12000, --maxPhasingReads:500, --maxClusteringReads:500},
}

What is the cleanest way to do this? I am a python programmer having to write code for a specific framework.

I know if I do: barcodes.each { println "${it.key} = ${it.value['LAA_minLength']}"}

I get barcode:LAA_minLength , but here I think LAA_minLength is a list and not a map.

How can I create a new map from this where {barcode:{LAA_minLength}} ?

bc_map = barcodes.collectEntries { key, val -> [(key) : val['LAA_minLength']]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM