Using Scala CLI, you can require the entire toolkit in a single line:
//> using toolkit latest
Alternatively, you can require just a specific version of UPickle:
//> using dep com.lihaoyi::upickle:4.1.0
In your build.sbt file, you can add the dependency on the Toolkit:
lazyvalexample=project.in(file(".")).settings(scalaVersion:="3.4.2",libraryDependencies+="org.scala-lang"%%"toolkit"%"0.7.0")
Alternatively, you can require just a specific version of UPickle:
libraryDependencies+="com.lihaoyi"%%"upickle"%"4.1.0"
In your build.sc file, you can add the dependency to the upickle library:
objectexampleextendsScalaModule{defscalaVersion="3.4.2"defivyDeps=Agg(ivy"org.scala-lang::toolkit:0.7.0")}
Alternatively, you can require just a specific version of UPickle:
ivy"com.lihaoyi::upickle:4.1.0"
Accessing values inside JSON
To parse a JSON string and access fields inside it, you can use uJson, which is part of uPickle.
The method ujson.read
parses a JSON string into memory:
valjsonString="""{"name": "Peter", "age": 13, "pets": ["Toolkitty", "Scaniel"]}"""valjson:ujson.Value=ujson.read(jsonString)println(json("name").str)// prints: Peter
To access the "name"
field, we do json("name")
and then call str
to type it as a string.
To access the elements by index in a JSON array,
valpets:ujson.Value=json("pets")valfirstPet:String=pets(0).strvalsecondPet:String=pets(1).strprintln(s"The pets are $firstPet and $secondPet")// prints: The pets are Toolkitty and Scaniel
You can traverse the JSON structure as deeply as you want, to extract any nested value. For instance, json("field1")(0)("field2").str
is the string value of field2
in the first element of the array in field1
.
JSON types
In the previous examples we used str
to type a JSON value as a string. Similar methods exist for other types of values, namely:
num
for numeric values, returningDouble
bool
for boolean values, returningBoolean
arr
for arrays, returning a mutableBuffer[ujson.Value]
obj
for objects, returning a mutableMap[String, ujson.Value]
importscala.collection.mutablevaljsonString="""{"name": "Peter", "age": 13, "pets": ["Toolkitty", "Scaniel"]}"""valjson=ujson.read(jsonString)valperson:mutable.Map[String, ujson.Value]=json.objvalage:Double=person("age").numvalpets:mutable.Buffer[ujson.Value]=person("pets").arr
If a JSON value does not conform to the expected type, uJson throws a ujson.Value.InvalidData
exception.
valname:Boolean=person("name").bool// throws a ujson.Value.InvalidData: Expected ujson.Bool (data: "Peter")