How to modify a JSON file?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DodoIta

Hi everyone,
I have a JSON file structured like this:

  "last_req": {
    "year": "2018",
    "month": "6",
    "day": "25",
    "hour": "12",
    "minute": "18"
  }

I can open the file and read it with no problems. But I’m trying (with no success) to save some changes to this file. The various “File.store…” functions seem to be more appropriate for storing data in empty files.
How can I modify this file from GDScript?

1 Like
:bust_in_silhouette: Reply From: Zylann

To modify a JSON file, you need to load it, modify its data, and then save it. That is, you need to overwrite the existing file with the new data:

# Load
var f = File.new()
f.open("data.json", File.READ)
var json = JSON.parse(f.get_as_text())
f.close()
var data = json.result

# Modify
data["year"] = 2012

# Save
f = File.new()
f.open("data.json", File.WRITE)
f.store_string(JSON.print(data, "  ", true))
f.close()

I’ve always overlooked JSON.print.
Thank you a lot, it works perfectly.

DodoIta | 2018-06-26 15:07

1 Like

What does .result does here? And what’s the godot 4 alternative for this???

I have an open source project made with Godot 4.2 that saves data in json format. See the save system here.

This is the save script. Keep in mind that this is my implementation for my specific needs. You may want to adapt it to yours.

Check the documentation of JSON and FileAccess to learn more.

extends Node

const FILE_NAME := "settings.json"
const FILE_PATH := "user://" + FILE_NAME

## JSON file is divided in categories in order to keep things organized.
const CATEGORIES_ROUTES := {
	categories.SETTINGS: "settings",
	categories.CROSSHAIR: "crosshair",
	categories.HIGH_SCORE: "high_score"
}

enum categories { SETTINGS, CROSSHAIR, HIGH_SCORE }

var game_data := {}

func _ready() -> void:
	load_all_data()

func save_data(key, value, category: categories, file_directory = FILE_PATH) -> void:
	var category_key = CATEGORIES_ROUTES[category]
	if !game_data.has(category_key):
		game_data[category_key] = {}
	game_data[category_key][key] = value
	var json = JSON.stringify(game_data, "\t")
	var file = FileAccess.open(file_directory, FileAccess.WRITE)
	file.store_line(json)
	file.close()

func save_all_data(file_directory: String = FILE_PATH) -> void:
	var json = JSON.stringify(game_data, "\t")
	var file = FileAccess.open(file_directory, FileAccess.WRITE)
	file.store_line(json)
	file.close()

func get_data(category: categories, key: String):
	var result = null
	if game_data != null \
		and game_data.has(CATEGORIES_ROUTES[category]) \
		and game_data.get(CATEGORIES_ROUTES[category]).has(key):
		result = game_data.get(CATEGORIES_ROUTES[category]).get(key)
	return result

func load_all_data(file_directory: String = FILE_PATH) -> void:
	var json = JSON.new()
	var result = {}
	var file = FileAccess.open(file_directory, FileAccess.READ)
	if file:
		json.parse(file.get_as_text())
		file.close()
	else:
		print("File not found.")

	if json.data != null:
		result = json.data
	game_data = result
1 Like