Skip to content

Latest commit

 

History

History
578 lines (440 loc) · 8.84 KB

BasicJsonExamples.md

File metadata and controls

578 lines (440 loc) · 8.84 KB

Basic JSON Operations

Create, read, update, delete (CRUD) operations with the Redis JSON data type

Contents

  1. Business Value Statement
  2. Create
    1. Key Value Pair
    2. Single String Property
    3. Multiple Properties
    4. Multiple Properties + Data Types
    5. JSON Arrays
    6. JSON Objects
    7. Mix
  3. Read
    1. Key Fetch
    2. Single Property Fetch
    3. Multi-Property Fetch
    4. Nested Property Fetch
    5. Array Fetch
  4. Update
    1. Entire Object
    2. Single Property
    3. Nested Property
    4. Array Item
  5. Delete
    1. Entire Object
    2. Single Property
    3. Nested Property
    4. Array Item

Business value statement

Document stores are a NoSQL database type that provide flexible schemas and access patterns familiar to developers. Redis natively provides document store functionality with its JSON data type. Hence, Redis complements existing document store databases such as MongoDB or provides standalone JSON document storage.

Create

Syntax

JSON.SET

Key-value pair

Insert a simple KVP as a JSON object.

Command

JsonCommands json = db.JSON();
Console.WriteLine(json.Set("ex1:1", "$", "\"val\""));

Result

True

Single string property

Insert a single-property JSON object.

Command

Console.WriteLine(json.Set("ex1:2", "$", new {field1 = "val1" }));

Result

True

Multiple Properties

Insert a JSON object with multiple properties.

Command

Console.WriteLine(json.Set("ex1:3", "$", new {
    field1 = "val1",
    field2 = "val2"
}));

Result

True

Multiple Properties + Data Types

Insert a JSON object with multiple properties of different data types.

Command

Console.WriteLine(json.Set("ex1:4", "$", new {
    field1 = "val1",
    field2 = "val2",
    field3 = true,
    field4 = (string?) null
}));

Result

True

JSON Arrays

Insert a JSON object that contains an array.

Command

Console.WriteLine(json.Set("ex1:5", "$", new {
    arr1 = new [] {"val1", "val2", "val3"}
}));

Result

True

JSON Objects

Insert a JSON object that contains a nested object.

Command

Console.WriteLine(json.Set("ex1:6", "$", new {
    obj1 = new {
        str1 = "val1",
        num2 = 2
    }
}));

Result

True

Mix

Insert a JSON object with a mixture of property data types.

Command

Console.WriteLine(json.Set("ex1:7", "$", new {
    str1 = "val1",
    str2 = "val2",
    arr1 = new [] {1,2,3,4},
    obj1 = new {
        num1 = 1,
        arr2 = new [] {"val1","val2", "val3"}
    }
}));

Result

True

Read

Syntax

JSON.GET

Key Fetch

Set and Fetch a simple JSON KVP.

Command

json.Set("ex2:1", "$", "\"val\"");
Console.WriteLine(json.Get(key: "ex2:1",
    path: "$",
    indent: "\t",
    newLine: "\n"
));

Result

[
        "val"
]

Single Property Fetch

Set and Fetch a single property from a JSON object.

Command

json.Set("ex2:2", "$", new {
    field1 = "val1"
});
Console.WriteLine(json.Get(key: "ex2:2",
    path: "$.field1",
    indent: "\t",
    newLine: "\n"
));

Result

[
        "val1"
]

Multi-Property Fetch

Fetch multiple properties.

Command

json.Set("ex2:3", "$", new {
    field1 = "val1",
    field2 = "val2"
});
Console.WriteLine(json.Get(key: "ex2:3",
    paths: new[] {"$.field1", "$.field2" },
    indent: "\t",
    newLine: "\n"
));

Result

{
        "$.field1":[
                "val1"
        ],
        "$.field2":[
                "val2"
        ]
}

Nested Property Fetch

Fetch a property nested in another JSON object.

Command

json.Set("ex2:4", "$", new {
    obj1 = new {
    str1 = "val1",
    num2 = 2
    }
});
Console.WriteLine(json.Get(key: "ex2:4",
    path: "$.obj1.num2",
    indent: "\t",
    newLine: "\n"
));

Result

[
        2
]

Array Fetch

Fetch properties within an array and utilize array subscripting.

Command

json.Set("ex2:5", "$",new {
    str1 = "val1",
    str2 = "val2",
    arr1 = new[] {1,2,3,4},
    obj1 = new {
        num1 = 1,
        arr2 = new[] {"val1","val2", "val3"}
    }
});
Console.WriteLine(json.Get(key: "ex2:5",
    path: "$.obj1.arr2",
    indent: "\t",
    newLine: "\n"
));
Console.WriteLine(json.Get(key: "ex2:5",
    path: "$.arr1[1]",
    indent: "\t",
    newLine: "\n"
));
Console.WriteLine(json.Get(key: "ex2:5",
    path: "$.obj1.arr2[0:2]",
    indent: "\t",
    newLine: "\n"
));
Console.WriteLine(json.Get(key: "ex2:5",
    path: "$.arr1[-2:]",
    indent: "\t",
    newLine: "\n"
));

Results

[
        [
                "val1",
                "val2",
                "val3"
        ]
]
[
        2
]
[
        "val1",
        "val2"
]
[
        3,
        4
]

Update

Syntax

JSON.SET

Entire Object

Update an entire JSON object.

Command

json.Set("ex3:1", "$", new {field1 = "val1"});
json.Set("ex3:1", "$", new {foo = "bar"});
Console.WriteLine(json.Get(key: "ex3:1",
    indent: "\t",
    newLine: "\n"
));

Result

{
        "foo":"bar"
}

Single Property

Update a single property within an object.

Command

json.Set("ex3:2", "$", new {
    field1 = "val1",
    field2 = "val2"
});
json.Set("ex3:2", "$.field1", "\"foo\"");
Console.WriteLine(json.Get(key: "ex3:2",
    indent: "\t",
    newLine: "\n"
));

Result

{
        "field1":"foo",
        "field2":"val2"
}

Nested Property

Update a property in an embedded JSON object.

Command

json.Set("ex3:3", "$", new {
    obj1 = new {
        str1 = "val1",
        num2 = 2
    }
});
json.Set("ex3:3", "$.obj1.num2", 3);
Console.WriteLine(json.Get(key: "ex3:3",
    indent: "\t",
    newLine: "\n"
));

Result

{
        "obj1":{
                "str1":"val1",
                "num2":3
        }
}

Array Item

Update an item in an array via index.

Command

json.Set("ex3:4", "$", new {
    arr1 = new[] {"val1", "val2", "val3"}
});
json.Set("ex3:4", "$.arr1[0]", "\"foo\"");
Console.WriteLine(json.Get(key: "ex3:4",
    indent: "\t",
    newLine: "\n"
));

Result

{
        "arr1":[
                "foo",
                "val2",
                "val3"
        ]
}

Delete

Syntax

JSON.DEL

Entire object

Delete entire object/key.

Command

json.Set("ex4:1", "$", new {field1 = "val1"});
json.Del("ex4:1");
Console.WriteLine(json.Get(key: "ex4:1",
    indent: "\t",
    newLine: "\n"
));

Result

Single Property

Delete a single property from an object.

Command

json.Set("ex4:2", "$", new {
    field1 = "val1",
    field2 =  "val2"
});
json.Del("ex4:2", "$.field1");
Console.WriteLine(json.Get(key: "ex4:2",
    indent: "\t",
    newLine: "\n"
));

Result

{
        "field2":"val2"
}

Nested property

Delete a property from an embedded object.

Command

json.Set("ex4:3", "$", new {
    obj1 = new {
        str1 = "val1",
        num2 = 2
    }
});
json.Del("ex4:3", "$.obj1.num2");
Console.WriteLine(json.Get(key: "ex4:3",
    indent: "\t",
    newLine: "\n"
));

Result

{
        "obj1":{
                "str1":"val1"
        }
}

Array item

Delete a single item from an array.

Command

json.Set("ex4:4", "$", new {
    arr1 = new[] {"val1", "val2", "val3"}
});
json.Del("ex4:4", "$.arr1[0]");
Console.WriteLine(json.Get(key: "ex4:4",
    indent: "\t",
    newLine: "\n"
));

Result

{
        "arr1":[
                "val2",
                "val3"
        ]
}