简体   繁体   中英

how to convert properties file to JSON using scala

How to convert properties file to JSON using scala

Properties file contains

a.b.10=C 
a.b.11=C50
a.b.12=C508

Output should be {"a":{"b":{"10":"C","11":"C50","12":"C508"}}}

You can use circe-config . Example:

import io.circe.config.parser.parse

val result = parse("""
  a.b.10=C 
  a.b.11=C50
  a.b.12=C508
""").map(_.noSpaces)

The example above will produce an Either[ParsingFailure, String] , which you can then destructure to handle failures, for example

result match {
  case Left(failure) => // handle parsing failure
  case Right(jsonString) => // do something with your json string
}

The json string produced by the example above is:

{"a":{"b":{"12":"C508","10":"C","11":"C50"}}}

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