Reading JSON files
Reading single fileβ
Start by importing scalaql
:
import scalaql._
// Docs classes
import scalaql.docs.Hogwarts._
import scalaql.docs.DocUtils._
// Imports for examples
import java.nio.file.Paths
import io.circe.generic.auto._
Assume you have a JSON file like the following:
val studentsPath = Paths.get("docs/src/main/resources/students.json")
// studentsPath: java.nio.file.Path = docs/src/main/resources/students.json
printFile(studentsPath)
// {"name":"Harry","age":19,"faculty":"Gryffindor","grade":85.1,"specialization":"learning","birthDay":"1980-07-31"}
// {"name":"Ron","age":18,"faculty":"Gryffindor","grade":66.2,"specialization":"eating","birthDay":"1980-05-01"}
// {"name":"Hermione","age":18,"faculty":"Gryffindor","grade":99.6,"specialization":"learning","birthDay":"1979-09-17"}
// {"name":"Draco","age":18,"faculty":"Slytherin","grade":85.1,"specialization":"trolling","birthDay":"1980-06-05"}
// {"name":"Cedric","age":17,"faculty":"Hufflepuff","grade":90.1,"specialization":"young dying","birthDay":"1977-10-01"}
First, you defined a Query
as usual:
val query =
select[Student]
.where(_.age >= 18)
// query: Query[From[Student], Student] = FROM(Hogwarts::Student) -> WHERE
Then you could specify to run the Query
on a json file instead of a Scala collection:
query
.show(truncate=false)
.run(
from(
json.read[Student].file(studentsPath)
)
)
// +--------+---+----------+-----+--------------+----------+
// |name |age|faculty |grade|specialization|birthDay |
// +--------+---+----------+-----+--------------+----------+
// |Harry |19 |Gryffindor|85.1 |learning |1980-07-31|
// |Ron |18 |Gryffindor|66.2 |eating |1980-05-01|
// |Hermione|18 |Gryffindor|99.6 |learning |1979-09-17|
// |Draco |18 |Slytherin |85.1 |trolling |1980-06-05|
// +--------+---+----------+-----+--------------+----------+
//