Ravikumar Reddy Yeruva
Ravikumar Reddy Yeruva

Reputation: 117

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"}}}

Upvotes: 0

Views: 324

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108159

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"}}}

Upvotes: 4

Related Questions