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:

import { JsonFilePersister } from "pip-services3-data-nodex";

using PipServices3.Data.Persistence;

import (
	persist "github.com/pip-services3-gox/pip-services3-data-gox/persistence"
)

import 'package:pip_services3_data/pip_services3_data.dart';

from pip_services3_data.persistence import JsonFilePersister

Not available

Creating the JSON persistence component

There are two ways to 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:

import { JsonFilePersister } from "pip-services3-data-nodex";

let persister = new JsonFilePersister<string>("./data.json");

using PipServices3.Data.Persistence;

var persister = new JsonFilePersister<string>();

import (
	persist "github.com/pip-services3-gox/pip-services3-data-gox/persistence"
)

persister := persist.NewJsonFilePersister[any]("./data.json")

import 'package:pip_services3_data/pip_services3_data.dart';

// Custom json serializable data
class MyData {
  String? key;
  String? value;

  MyData({this.key, this.value});

  void fromJson(item) {
    key = item['key'];
    value = item['value'];
  }

  Map<String, String?> toJson() => {'key': key, 'value': value};
}

var persister = JsonFilePersister<MyData>('./data.json');

from pip_services3_data.persistence import JsonFilePersister

persister = JsonFilePersister("E:\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.

import { ConfigParams } from "pip-services3-commons-nodex";
import { JsonFilePersister } from "pip-services3-data-nodex";

let persister = new JsonFilePersister<string>();
let myConfig = ConfigParams.fromTuples("path", "./data.json");
persister.configure(myConfig);

using PipServices3.Data.Persistence;
using System.Collections.Generic;

var persister = new JsonFilePersister<string>();
var myConfig = ConfigParams.FromTuples("path", "./data.json");
persister.Configure(myConfig);

import (
	config "github.com/pip-services3-gox/pip-services3-commons-gox/config"
	persist "github.com/pip-services3-gox/pip-services3-data-gox/persistence"
)

persister := persist.NewJsonFilePersister[any]("")
myConfig := config.NewConfigParamsFromTuples("path", "./data.json")
persister.Configure(context.Background(), myConfig)
import 'package:pip_services3_commons/pip_services3_commons.dart';
import 'package:pip_services3_data/pip_services3_data.dart';

// Custom json serializable data
class MyData {
  String? key;
  String? value;

  MyData({this.key, this.value});

  void fromJson(item) {
    key = item['key'];
    value = item['value'];
  }

  Map<String, String?> toJson() => {'key': key, 'value': value};
}


var persister = JsonFilePersister<MyData>();
var myConfig = ConfigParams.fromTuples(['path', './data.json']);
persister.configure(myConfig);

from pip_services3_data.persistence import JsonFilePersister
from pip_services3_commons.config import ConfigParams

persister = JsonFilePersister()
my_config = ConfigParams.from_tuples("path", "E:\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 correlation_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.

await persister.save("123", ["A1", "B1", "C1"]);

await persister.SaveAsync("123", new List<string>() { "A1", "B1", "C1" });

persister.Save(context.Background(), "123", []interface{}{"A1", "B1", "C1"})

await persister.save('123', <MyData>[
    MyData(key: 'key1', value: 'value1'),
    MyData(key: 'key2', value: 'value2')
]);

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 correlation_id parameter as input. The code below explains its usage.

let items = await persister.load("123");

var items = await persister.LoadAsync("123");

items, _ := persister.Load(context.Background(), "123")

var items = await persister.load('123');

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.

import { JsonFilePersister } from "pip-services3-data-nodex";

// Create the JSON persistence component
let persister = new JsonFilePersister<string>("./data.json");

// Save data on the JSON persistence object
await persister.save("123", ["A1", "B1", "C1"]);

// Read data from the JSON persistence object
let items = await persister.load("123");

console.log(items);
// Create the JSON persistence component
var persister = new JsonFilePersister<string>("./data.json");

// Save data on the JSON persistence object
await persister.SaveAsync("123", new List<string>() { "A1", "B1", "C1" });

// Read data from the JSON persistence object
var items = await persister.LoadAsync("123");

Console.WriteLine("[{0}]", string.Join(", ", items));
import (
	"fmt"
	persist "github.com/pip-services3-gox/pip-services3-data-gox/persistence"
)

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

fmt.Print(items)
import 'package:pip_services3_data/pip_services3_data.dart';

// Custom json serializable data
class MyData {
  String? key;
  String? value;

  MyData({this.key, this.value});

  void fromJson(item) {
    key = item['key'];
    value = item['value'];
  }

  Map<String, String?> toJson() => {'key': key, 'value': value};
}

// Create the JSON persistence component
var persister = JsonFilePersister<MyData>('./data.json');

// Save data on the JSON persistence object
await persister.save('123', <MyData>[
  MyData(key: 'key1', value: 'value1'),
  MyData(key: 'key2', value: 'value2')
]);

// Read data from the JSON persistence object
var items = await persister.load('123');

print(items);
from pip_services3_data.persistence import JsonFilePersister

# Create the JSON persistence component
persister = JsonFilePersister("E:\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.