You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/toolkit/upickle-modify-jsons.md
+41-41Lines changed: 41 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -9,59 +9,59 @@ next-page:
9
9
10
10
{% include markdown.html path="_markdown/install-upickle.md" %}
11
11
12
-
## Modifying jsons
13
-
If you want to set, modify or remove fields of a json, you can do it with Scala Toolkit.
14
-
First, you have to load the json. You can either do it from a json loaded to a `String`,
15
-
or operate on json generated from Scala values.
16
-
17
-
## Loading a json text and modyfing it
18
-
To read the `json` from text, you can use the `ujson.read` function.
19
-
This function returns an object representing the json that you can operate on.
20
-
Adding new fields and updating their values is done as a simple assignment:
12
+
The `ujson.read` method creates a mutable representation of JSON that you can update, by adding, modifying or removing any field and element of its JSON objects and arrays.
13
+
First you need to read the JSON string, then you can update it, and finally you can write it back to a String.
In the above code example, we change the value of the field `"name"`, from `"John"` to `"Peter"`, and we add a new field `"surname"` with value `"Scalinsky"`.
43
+
37
44
## Removing fields
38
-
To remove fields from json you need to declare whether you are removing them from an `Object` or an `Array`.
39
-
- To remove a field from an object, you can use the `.obj.remove("name")` function on json object
40
-
- To remove a field from an array, you can use the `.obj.remove(index)` function on json object
41
-
Following the previous example, below you can see how to remove some fields from it.
45
+
46
+
To remove a field from a JSON object:
47
+
- declare that the JSON value is an object by calling the `obj` method
48
+
- call the `remove` method with the name of the field to remove.
json("pets").arr.remove(1) // remove pet with index 1 ("Scaniel")
46
-
valjsonString:String= ujson.write(json)
47
-
println(jsonString) // prints {"pets":["Toolkitty"]}
52
+
json.obj.remove("name") // Remove "name" field from object
53
+
println(ujson.write(json))
48
54
```
49
-
Above, we first removed the field `name` from the top-level object in the json.
50
-
Then, we selected the array `pets` and removed the value with index `1` from it.
51
55
52
-
## Operating on jsons generated from Scala values
53
-
If you want to operate on jsons generate from Scala values (like in the [How to write JSONs]({% link _overviews/toolkit/upickle-write-json.md %}) tutorial), then it is possible as well.
54
-
Usually, `upickle.write` operation outputs a `String`. But, if you replace it with `upickle.writeJs`, then it returns a json represention from `ujson`.
55
-
Then you can operate on it in the same way as you did in the previous code snippets in this tutorial. For example:
description: How to read a JSON to typed structure with Scala Toolkit
5
5
num: 21
@@ -9,18 +9,18 @@ next-page: upickle-write-json
9
9
10
10
{% include markdown.html path="_markdown/install-upickle.md" %}
11
11
12
-
In Scala Toolkit, it is possible to read a JSON and values inside of it in two ways:
13
-
- First one offers everything you need to quickly extract data from a JSON, without requiring any specific structure.
14
-
This approach is described in the [How to read a JSON]({% link _overviews/toolkit/upickle-read-json.md %}) tutorial. Visit it if you want a simple and fast way to read JSONs.
15
-
- The second approach one allows you to work with the json in a fully typed code, and even to provide your custom data structures.
16
-
This approach is described in this tutorial. It is more well-suited when you plan on reusing, storing and operating on the structures loaded from jsons.
17
-
18
-
## Reading JSONs to a typed structure
19
-
To perform typed operations on JSONs, you can utilize a `upickle` Toolkit library.
20
-
This approach has an advantage of additional safety and convenience of working with the structured data.
21
-
As an example - if you know that your JSON contains a set of fields, where each field's value is an array of numbers,
22
-
you can use the `read[Map[String, List[Int]]](json)` function that will return a map of this exact type.
23
-
You can see an example of that below
12
+
In the Scala Toolkit, there are two ways of reading JSON: the dynamic way and the fully-typed way.
13
+
- In the first approach, we can quickly extract data from JSON, without requiring any specific schema.
14
+
To discover this approach read the [How to read JSON dynamically]({% link _overviews/toolkit/upickle-read-json.md %}). It is simple and fast.
15
+
- In the second approach, you define your own data type, and transform JSON objects into instances of that data type, making sure that all the required fields are defined with valid types.
16
+
This approach is described below.
17
+
It is well-suited when you need to validate the JSON values, to store them or to operate on them in a safe way.
18
+
19
+
## Parsing a JSON object to a Map
20
+
To type a JSON object into a Scala `Map`, you can use the `upickle` library.
21
+
This approach is safe and convenient for working with structured data.
22
+
It allows you to quiclky validate the structure of the JSON string.
In Scala, you can use a `case class` to define your own data type. For example, if you wanted to represent a Person with names of its pets, you could do it as follows:
32
+
In this example, we expect the JSON string to contain an object, where each fields's value is an array of numbers.
33
+
We call the `upickle.default.read` with the type parameter `Map[String, List[Int]]` to transform the JSON object into a map of this exact type.
34
+
35
+
If the JSON value does not match the expected type, upickle throws an exception.
36
+
37
+
### Parsing a JSON object to a custom data type
38
+
39
+
In Scala, you can use a `case class` to define your own data type.
40
+
For example, to represent the pets and their owner, you can do it as follows:
After defining this `case class`, you can read a JSON containing its fields. But first, you need to provide an instance of `ReadWriter` that will tell the library
38
-
how to handle this type. Luckily, `upickle` is able to fully automate that and all have to do is:
44
+
45
+
To be able to read a `PetOwner` from a JSON we need to provde a given instance of `ReadWriter[PetOwner]`.
46
+
Luckily, `upickle` is able to fully automate that and all you need to write is:
47
+
39
48
```scala
40
49
givenReadWriter[PetOwner] = macroRW
41
50
```
42
-
`given` keyword may appear strange at first, but it just says that this value may be used later transparently in your code by some functions that needs a `ReadWriter[PetOwner]`.
43
-
You don't need to think about it for too long, that's the only thing you need to do - as long as this value is available, you will be able to read a JSON that conforms to the type of a `PetOwner`.
44
-
The second part of this definition, `macroRW`, automates everything that needs to be done to provide this mechanism capable of reading these JSONs.
45
-
After this declaration, you can put everything together and read a JSON as you inteded to:
51
+
52
+
The `given` keyword may appear strange at first but is very powerful.
53
+
Having a `given ReadWriter[PetOwner]` in the current scope allows you to ask the `upickle.default.read` method to return a value of `PetOwner`, like this `read[PetOwner](jsonString)`.
54
+
As long as the given value is available, you will be able to read JSON that conforms to the type of a `PetOwner`.
55
+
The second part of this definition, `macroRW`, automates the instanciation of `ReadWriter` so that we don't have to do it by hand.
56
+
57
+
After this declaration, you can put everything together and read a `PetOwner` from JSON:
println(s"$ownerName has a pet called $ownerFirstAnimal")
56
-
// Prints "Peter has a pet called Toolkitty
57
-
```
67
+
valfirstPet= petOwner.pets.head
68
+
println(s"${petOwner.name} has a pet called $firstPet")
69
+
// Prints "Peter has a pet called Toolkitty"
70
+
```
71
+
72
+
### Making the given ReadWriter globally available
58
73
59
-
### Providing ReadWriter in the companion object
74
+
The given `ReadWriter[PetOwner]` is only available in the scope in which it is defined.
75
+
To make it available globally you can define it in the companion object of `PetOwner`.
60
76
61
-
If you want to have the `ReadWriter[PetOwner]` always available with the `PetOwner`, no matter where in your project you are, you can use a Scala's feature called `companion objects`.
62
-
Next to the `PetOwner` case class, you need to put an `object` that is called the same - `object PetOwner`. In that object, you can put the `given` ReadWriter.
63
-
After you do that, the `ReadWriter` will be always available alongside the `PetOwner` case class.
description: How to read a JSON with Scala Toolkit.
4
+
description: How to read JSON with Scala Toolkit.
5
5
num: 20
6
6
previous-page: upickle-intro
7
7
next-page: upickle-read-json-typed
8
8
---
9
9
10
10
{% include markdown.html path="_markdown/install-upickle.md" %}
11
11
12
-
In Scala Toolkit, it is possible to read a JSON and values inside of it in two ways:
13
-
- First one offers everything you need to quickly extract data from a JSON, without requiring any specific structure. This approach is described in this article. It is a simple and fast way to read JSONs.
14
-
- While the second approach allows you to work with the json in a fully typed code, and even to provide your custom data structures. To discover this approach read the [How to read a JSON to typed structure]({% link _overviews/toolkit/upickle-read-json-typed.md %}) tutorial. It is more well-suited when you plan on reusing, storing and operating on the structures loaded from jsons.
12
+
In the Scala Toolkit, there are two ways of reading JSON: the dynamic way and the fully-typed way.
13
+
- In the first approach, we can quickly extract data from JSON, without requiring any specific schema.
14
+
This approach is described below.
15
+
It is simple and fast.
16
+
- In the second approach, you define your own data type, to parse JSON objects into instances of that data type, making sure that all the required fields are defined with the expected types.
17
+
To discover this approach read the [How to parse a JSON value to a data type]({% link _overviews/toolkit/upickle-read-json-typed.md %}).
18
+
It is well-suited when you need to validate the JSON values, to store them or to operate on them in a safe way.
15
19
16
-
## Reading JSONs dynamically
17
-
If you want to just parse a JSON and access some of its field, you may just use a `ujson` library that is included with Toolkit.
18
-
Function `ujson.read` allows you to read a JSON and access it afterwards.
19
-
You can access the fields in the returned JSON object by just providing their names exactly as you would to a standard function.
20
-
Afterwards you have to declare what you expect to be inside the field and extract it, for example `str` extracts field's value as a `String`.
21
-
For example, code below prints out `Peter`.
20
+
## Reading JSON dynamically
21
+
If you want to parse a JSON object and access some of its field, you can use the uJson library, which is brought by uPickle.
22
+
23
+
The method `ujson.read` can parse a JSON string and make all of its fields available.
22
24
```scala
23
25
valjsonString="""{"name": "Peter", "age": 13}"""
24
26
valjson= ujson.read(jsonString)
25
27
println(json("name").str) // Prints out "Peter"
26
28
```
27
-
You can operate on arrays similarly, accessing the indices as in the example below.
29
+
30
+
You can access a field by passing its name to the `json` value, and then specifying which type you expect.
31
+
If `name` is a field that should contain a string, you can access it using `json("name").str`.
32
+
33
+
Similarly, you can access an element of an array by passing its indice, as in the example below:
0 commit comments