Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Convert a string into a map

Tags:

json

map

scala

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.

like image 435
Learner Avatar asked Nov 12 '13 19:11

Learner


3 Answers

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
like image 189
Yann Moisan Avatar answered Oct 25 '22 11:10

Yann Moisan


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.

like image 41
sventechie Avatar answered Oct 25 '22 12:10

sventechie


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.

like image 2
Andrey Chaschev Avatar answered Oct 25 '22 11:10

Andrey Chaschev