简体   繁体   中英

Best way to change JSON keys with JACKSON or other java lib

I have a Json file or string for example:

{
  "my-key0": "ke0",
  "key-Arr": [
    {
      "nested-key1": {
        "value": "val",
        "seqno": 12
      },
      "nested2": 1
    },
    {
      "dns-sss-qqq": [
        {
          "some": "aaaaa"
        }
      ]
    }
  ],
  "recsize": 459,
  "my-obj": {
    "my-key1": {
      "my-key2": "key2"
    }
  }
}

My purpose is to replace "-" char to "_" char only in keys in Scala/Java.

In first I thought it can be done with REGEX but the keys can be UNQUOTED and it also can effect on values. What is most efficient way to it?(Performance is matter) I have to process GBs of such records. Thank you

Try jsoniter-scala - it supports kebab-case since v0.17.0 and also it is more efficient in parsing and serialization than jackson-module-scala .

Here are latest results of benchmarks which compare parsing & serialization performance of jsoniter-scala vs. jackson-module-scala , circe and play-json libraries using JDK 8.

Also it has ability to parse streaming JSON values and JSON arrays from java.io.InputStream w/o need of holding all parsed values in the memory.

Extraction of some selected fields or substructures instead of parsing whole message or document is where jsoniter-scala shines.

So try just use it instead of conversion of all your data.

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