java - Flatten JSON into Specific Format -


i'd use java take in json body, iterate through records, , output every other record new json array. each set of 2 records own array, , need take first 1 in each respective array. i'll providing column names in original request need added subsequent flattened object. example, let's have following json request body:

{   "records": [     [         [             "0dfc29e2-700e-4cc1-931e-b61df4954b6b",             "john doe",             "teacher",             "china"         ],         [             "b5b9186e-ce65-4911-8516-c510d3cc3ace",             "jane doe",             "doctor",             "london"         ]     ],     [         [             "20c4dd07-4e96-47f8-a1e1-b20b4c48120c",             "jim doe",             "lawyer",             "canada"         ],         [             "76718cb1-238f-418e-bd14-5e2867ff3fb4",             "jack doe",             "chef",             "mexico"         ]      ]    ],     "columns": [      "id",      "name",      "occupation",      "location"    ] } 

i'd request body flattened following:

[{   "id": "0dfc29e2-700e-4cc1-931e-b61df4954b6b",   "name": "john doe",   "occupation": "teacher",   "location": "china" }, {   "id": "20c4dd07-4e96-47f8-a1e1-b20b4c48120c",   "name": "jim doe",   "occupation": "lawyer",   "location": "canada" }] 

i'd code pretty dynamic, doesn't explicitly reference column names in code. way can pass other column names in future if have different json body structure, , work accordingly. i'll passing data title of "records" that's okay hardcode. appreciated.

you should convert source json collection of maps. each map contain property names , property values. after that, can serialize expected format. in below example use jackson library, think should able use gson library too.

at first, should define sourceentity class define properties input json.

class sourceentity {      private string[][][] records;     private string[] columns;      public string[][][] getrecords() {         return records;     }      public void setrecords(string[][][] records) {         this.records = records;     }      public string[] getcolumns() {         return columns;     }      public void setcolumns(string[] columns) {         this.columns = columns;     } } 

after that, should write converter, can parse input json, convert arrays collection of maps , serialize target json.

class jsonconverter {      private objectmapper objectmapper = new objectmapper();     private jsonfactory jsonfactory = new jsonfactory();      public string convert(file sourcejsonfile) throws exception {         sourceentity sourceentity = parsesourceentity(sourcejsonfile);         list<map<string, string>> result = converttotargetpropertiesmap(sourceentity);          return objectmapper.writevalueasstring(result);     }      private sourceentity parsesourceentity(file sourcejsonfile)             throws exception {         jsonparser parser = jsonfactory.createjsonparser(sourcejsonfile);         return objectmapper.readvalue(parser, sourceentity.class);     }      private list<map<string, string>> converttotargetpropertiesmap(             sourceentity entity) {         list<map<string, string>> list = new arraylist<map<string, string>>();         (string[][] pairs : entity.getrecords()) {             list.add(createpropertymap(entity.getcolumns(), pairs[0]));         }         return list;     }      private map<string, string> createpropertymap(string[] names,             string[] values) {         map<string, string> propertymap = new linkedhashmap<string, string>();         (int = 0; < values.length; i++) {             propertymap.put(names[i], values[i]);         }         return propertymap;     } } 

finally, should write little test:

import java.io.file; import java.util.arraylist; import java.util.linkedhashmap; import java.util.list; import java.util.map;  import com.fasterxml.jackson.core.jsonfactory; import com.fasterxml.jackson.core.jsonparser; import com.fasterxml.jackson.databind.objectmapper;  public class jacksonprogram {      public static void main(string[] args) throws exception {         jsonconverter converter = new jsonconverter();         string result = converter.convert(new file("/tmp/source.json"));         system.out.println(result);     } } 

above program prints json example input:

[{"id":"0dfc29e2-700e-4cc1-931e-b61df4954b6b","name":"john doe","occupation":"teacher","location":"china"},{"id":"20c4dd07-4e96-47f8-a1e1-b20b4c48120c","name":"jim doe","occupation":"lawyer","location":"canada"}] 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -