Skip to content

Latest commit

 

History

History
33 lines (27 loc) · 901 Bytes

json-modify.md

File metadata and controls

33 lines (27 loc) · 901 Bytes
title type description num previous-page next-page
How to modify JSON?
section
Modifying JSON with uPickle.
18
json-parse
json-deserialize

{% include markdown.html path="_markdown/install-upickle.md" %}

ujson.read returns a mutable representation of JSON that you can update. Fields and elemnts can be added, modified, or removed.

First you read the JSON string, then you update it in memory, and finally you write it back out again.

{% tabs 'modify' %} {% tab 'Scala 2 and 3' %}

// Parse the JSON string
val json: ujson.Value = ujson.read("""{"name":"John","pets":["Toolkitty","Scaniel"]}""")

// Update it
json("name") = "Peter"
json("nickname") = "Pete"
json("pets").arr.remove(1)

// Write it back to a String
val result: String = ujson.write(json)
println(result)
// prints: {"name":"Peter","pets":["Toolkitty"],"nickname":"Pete"}

{% endtab %} {% endtabs %}