JavaScriptObjectNotationTemplate
Package: Libraries.Data.Formats · Inherits from: Libraries.Language.Object
The JavaScriptObjectNotationTemplate class edits the top-level values of a JSON object without rebuilding the text. It finds where each top-level value starts and ends, and on output replaces only the values that were changed: every other byte (spacing, key order, number formats) stays as it was. This matters for files another program reads back with exact expectations, such as Bambu Studio's project_settings.config. It also works as a small reader: GetText returns a nested object's raw JSON text, which can be loaded into another template.
Example Code
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
class Main
action Main
text dq = ""
dq = dq:GetDoubleQuote()
JavaScriptObjectNotationTemplate json
json:Load("{" + dq + "a" + dq + ": " + dq + "1" + dq + "}")
json:Fill("a", "2") // only this value is rewritten
json:Put("added", "yes") // a missing key is appended
output json:ToText()
end
end
Actions
Count(text key)
The number of items in a list value.
Parameters
keytext: The key to count.
Returns
integer: The list's length, 1 for a single value, 0 if the key is missing.
Example
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
output settings:Count("filament_colour") + " filaments"
Fill(text key, text value)
Changes an existing key to a string value. A list stays a list of the same length with every item set to the value. Missing keys are left alone (use Has to find out, or Put to add them).
Parameters
keytext: The key to change.valuetext: The new value, written as a JSON string.
Example
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
settings:Fill("layer_height", "0.16")
file:Write(settings:ToText())
FillList(text key, Libraries.Containers.Array<text> items)
Changes an existing key to a list of exactly these strings, whatever its length was. Missing keys are left alone.
Parameters
keytext: The key to change.itemsLibraries.Containers.Array<text>: The new list items.
Example
use Libraries.Containers.Array
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
Array<text> colours
colours:Add("#FF8000")
settings:FillList("filament_colour", colours)
GetKeys()
Every top-level key, in file order.
Returns
Libraries.Containers.Array<text>: A new array of the keys.
Example
use Libraries.Containers.Array
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
Array<text> keys = settings:GetKeys()
output "The first setting is " + keys:Get(0)
GetText(text key, text fallback)
The value of a key as text: a string without its quotes, a number or literal as written, a nested object as its raw JSON text, and for a list its first string item.
Parameters
keytext: The key to read.fallbacktext: What to return if the key is missing (or a list has no string item).
Returns
text: The value, or the fallback.
Example
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
output "Layer height: " + settings:GetText("layer_height", "0.2")
Has(text key)
Checks whether the object has a top-level key.
Parameters
keytext: The key to look for.
Returns
boolean: True if the key exists.
Example
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
if not settings:Has("layer_height")
output "This isn't a print settings file."
end
Load(text json)
Reads JSON text whose top level is an object, and finds every top-level key. Stops with a ParseError giving the line of the problem if the text isn't a JSON object.
Parameters
jsontext: The JSON text.
Example
use Libraries.Containers.Array
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
Array<text> keys = settings:GetKeys()
output keys:GetSize() + " settings"
Put(text key, text value)
Sets a key to a single string value, adding it at the end of the object if it is missing.
Parameters
keytext: The key to set.valuetext: The new value, written as a JSON string.
Example
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
settings:Put("print_settings_id", "0.20mm Standard")
ToText()
The JSON text with every change applied, ending in a line feed.
Returns
text: The edited JSON text.
Example
use Libraries.System.File
use Libraries.Data.Formats.JavaScriptObjectNotationTemplate
File file
file:SetPath("project_settings.json")
JavaScriptObjectNotationTemplate settings
settings:Load(file:Read())
settings:Fill("layer_height", "0.16")
File copy
copy:SetPath("project_settings_016.json")
copy:Write(settings:ToText())