6

What is the fastest way to convert this

{"a":"ab","b":"cd","c":"cd","d":"de","e":"ef","f":"fg"}

into mutable map in scala ? I read this input string from ~500MB file. That is the reason I'm concerned about speed.

Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
Learner
  • 1,685
  • 6
  • 30
  • 42

4 Answers4

15

If your JSON is as simple as in your example, i.e. a sequence of key/value pairs, where each value is a string. You can do in plain Scala :

myString.substring(1, myString.length - 1)
        .split(",")
        .map(_.split(":"))
        .map { case Array(k, v) => (k.substring(1, k.length-1), v.substring(1, v.length-1))}
        .toMap
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
2

The fastest way to read tree data structures in XML or JSON is by applying streaming API: Jackson Streaming API To Read And Write JSON.

Streaming would split your input into tokens like 'beginning of an object' or 'beginning of an array' and you would need to build a parser for these token, which in some cases is not a trivial task.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
2

That looks like a JSON file, as Andrey says. You should consider this answer. It gives some example Scala code. Also, this answer gives some different JSON libraries and their relative merits.

Community
  • 1
  • 1
sventechie
  • 1,859
  • 1
  • 22
  • 51
-1

Keeping it simple. If reading a json string from file and converting to scala map

import spray.json._
import DefaultJsonProtocol._

val jsonStr = Source.fromFile(jsonFilePath).mkString
val jsonDoc=jsonStr.parseJson
val map_doc=jsonDoc.convertTo[Map[String, JsValue]]

// Get a Map key value
val key_value=map_doc.get("key").get.convertTo[String]

// If nested json, re-map it.
val key_map=map_doc.get("nested_key").get.convertTo[Map[String, JsValue]]
println("Nested Value " + key_map.get("key").get)
Ajit Surendran
  • 709
  • 7
  • 4