简体   繁体   中英

Java: Best way to compare all keys within array of json objects?

I'm parsing json files (size varies from few KB to few GB) that are built as an array of objects, for example:

{  
  "records": [
    {
       "col1": "someValue",
       "col2": "someValue",
       "col3": "someValue", 
    },
    {
       "col1": "someValue",
       "col2": "someValue",
       "col3": "someValue",
    },
    {
       "col1": "someValue",
       "col2": "someValue",
       "col3": "someValue",
    }  
  ]
}

The records represent individual rows of data from a table and the file always contains data for one table only.

I can extract table's metadata and parse it without any issues. I'm using JSON.simple library to do this.

What I'm trying to do now, is to validate that all of the objects have the same keys, no more or less as data needs to be ingested into a table. I can extract the keys using keySet() method and put it into list but it seems like comparing one list to an another times amount of rows (from few to millions) is a very poor and costly implementation.

Is there some nice solution that could quickly compare all keys from all json objects in json array?

您无法避免必须查看数据的每一行(数量m)的每个键(数量n),因此无论如何复杂性都不能低于O(n * m)

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