简体   繁体   中英

How in groovy (java) automatically find out the json field type and return values from json, replacing null with empty space?

The question is still relevant!

In my task, json comes to my input, which I do not know in advance. I need to collect all json field types into "types" and return all values using reader.outputLines . Now the list of json field types is formed like this: def types = list.find (). Values () *. GetClass () *. SimpleName def types = list.find (). Values () *. GetClass () *. SimpleName def types = list.find (). Values () *. GetClass () *. SimpleName But I am faced with a problem when the same field in the first json block is null, and in the second the integer and the type is defined as null, and not as Integer.

  1. How to make sure that the type is determined by going through all the json blocks for each field, and not making an output based on the first block, and if everything is null or "" (empty), assign the default String?
  2. When returning values from json using reader.outputLines , replace null with "" (empty)?
import groovy.json.JsonSlurper
import ru.itrpro.xm.plugins.groovy.ResultSetReader;

class XM_PARSE_XLS {

    def execute(ResultSetReader reader, String pfile) {

        def jsonSlurper = new JsonSlurper()
        def list = jsonSlurper.parseText pfile

        List names = list.inject( new LinkedHashSet<>() ){ res, map ->
            res.addAll map.keySet()
            res
        }.toList()
        def types = list.find().values()*.getClass()*.simpleName

        //formation of the dataset header
        reader.outputLinesSetHeaders(names,types);

        list.each{ e ->
            reader.outputLines names.collect{ e[ it ] }
            //println names.collect{ e[ it ] }
        }

        //closing dataset
        reader.outputLinesEnd();
        return null;

    }
    static void main(String... args) {
        String pfile =  """
[{"AUTO":"bmw",
  "HOME":null,
  "JOB":""},
  
  {"AUTO":"audi",
  "HOME":135,
  "JOB":null},
  
  {"AUTO":"opel1",
  "HOME":10,
  "JOB":null}]
"""
        def SSC = new XM_PARSE_XLS()
        def res = SSC.execute(new ResultSetReader(), pfile)
    }
}

I'd suggest the following strategy: Iterate as long over the rows until you know each type or you reach some sort of end (depending on the amount of rows, you might want to just look in all rows or just give up after N rows). For each row try to determine the type and keep book of the type, but only "promote" from null to the type in your bookkeeping.

import groovy.json.JsonSlurperClassic

def data = new JsonSlurperClassic().parseText("""
[{"AUTO":"bmw",   "HOME":null, "JOB":""},
 {"AUTO":"audi",  "HOME":135,  "JOB":null},
 {"AUTO":"opel1", "HOME":10,   "JOB":null}]
""")

def types = [:]
def it = data.iterator() // or only take(10) e.g.
while (it.hasNext() && !(types && types.every{ _, v -> v })) {
    it.next().collectEntries{ k, v -> 
        [k, v?.getClass()?.simpleName] 
    }.inject(types, { m, entry ->
        if (entry.value!=null || !m.containsKey(entry.key)) {
            m[entry.key] = entry.value
        }
        return m
    })
}

// if there are types missing (values with null), replace them here with
// your default)

println types
// → [AUTO:String, JOB:String, HOME:Integer]

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