JSON persistence

How to persist data using a JSON file.

Key takeaways

JsonFilePersister Component in the data module, persistence library used to persist data in JSON format.
configure Method used to pass the path to a JSON file.
save Method used to save data to a JSON file.
load Method used to extract data from a JSON file.

Introduction

This tutorial will help you understand how to create a JSON persistence component. It starts by explaining the pre-requisites. Then it continues with an explanation on how to create a JSON persistence object, save data to it, and extract stored data from it. In the end, it provides an example where all the explained methods are included.

Persisting data in a JSON file

Pre-requisites

In order to create a JSON persistence component, we need to import the JsonFilePersister class. This class belongs to the persistence library in the data module. To include this class use the following command:

Not available
Not available
import (
	persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)
Not available
from pip_services4_persistence.persistence import JsonFilePersister

Not available

Creating the JSON persistence component

There are two ways to create a JSON persistence component. The first consists of declaring an instance of the JsonFilePersister class and passing the JSON file’s path to the constructor. Our code will be as follows:

Not available
Not available
import (
	persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)

persister := persist.NewJsonFilePersister[any]("./data.json")
Not available
from pip_services4_persistence.persistence import JsonFilePersister

persister = JsonFilePersister("data.json")


Not available

The second uses a config object to define the JSON file path. In this case, we need to define a ConfigParams object with the path and use the configure method to pass this object to the JSON persistence object.

Not available
Not available
import (
	"context"

	config "github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
	persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)

persister := persist.NewJsonFilePersister[any]("")
myConfig := config.NewConfigParamsFromTuples("path", "./data.json")
persister.Configure(context.Background(), myConfig)
Not available
from pip_services4_persistence.persistence import JsonFilePersister
from pip_services4_components.config import ConfigParams

persister = JsonFilePersister()
my_config = ConfigParams.from_tuples("path", "data.json")
persister.configure(my_config)

Not available

Saving data on a JSON file

To store data in a JSON file we use the save method. The first parameter is the trace_id, which is used to trace execution through the call chain. The second one is the JSON value we want to save. The following example shows how to use it.

Not available
Not available
persister.Save(context.Background(), []interface{}{"A1", "B1", "C1"})
Not available
persister.save(None, ["A1", "B1", "C1"])

Not available

Loading data from a JSON file

To load data from a JSON file we use the load method, which returns the loaded items. This method has the trace_id parameter as input. The code below explains its usage.

Not available
Not available
items, _ := persister.Load(context.Background())
Not available
items = persister.load(None)

Not available

Example

In the following example, we put everything together. First, we create a JSON persistence object, then we save some data to the JSON file, and lastly, we extract the stored data from the JSON file.

Not available
Not available
import (
	"context"
	"fmt"

	persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)

// Create the JSON persistence component
persister := persist.NewJsonFilePersister[any]("./data.json")
// Save data on the JSON persistence object
persister.Save(context.Background(), []interface{}{"A1", "B1", "C1"})
// Read data from the JSON persistence object
items, _ := persister.Load(context.Background())

fmt.Print(items)
Not available
from pip_services4_persistence.persistence import JsonFilePersister

# Create the JSON persistence component
persister = JsonFilePersister("data.json")

# Save data on the JSON persistence object
persister.save("123", ["A", "B", "C"])

# Read data from the JSON persistence object
items = persister.load("123")
print(items)

Not available

After running it, our output will be:

figure 1

And, our data.json file will contain:

figure 2

Wrapping up

In this tutorial, we have learned how to persist data and how to extract persisted data from a JSON file. In the end, we saw an example that summarizes all JSON persistence operations.