Parent

Parent can be used to step one step back from the current node. It can be useful for introducing data that are outside of current scope to the transformation.

Syntax

json_object parent(any $entry)

Returns the first json object that is a parent of $entry.
If $entry has no object as an parent, null is returned.

Parameters

$entry: The array, object, or key-value pair the parent of which will be retrieved.

Example

{
    "a": {           //This is the parent of "b" & "c"
        "b": 1,
        "c": {         //This is the child of "a" and parent of "d"
              "d": 2
        }
    }
}
{
    withoutparent: a.b,
    withparent: parent(a.b)
}
{
    "withoutparent": 1,
    "withparent": {
        "b": 1,
        "c": {
            "d": 2
        }
    }
}

In this example, the functionality the parent function introduces is demonstrated. When the same scope (a.b) is used alone, its value (1) is returned, while when it is used within the parent function, it results in the value of its parent (a).

Notes

The parent function can be combined with the @-operator to retrieve the parent of the current node. Similarly, parent can be also used in combination with the $-operator, to navigate through the already defined keys, objects, and arrays in the transformation. An example that utilizes the same data from above can be seen below:

{
    Object: {             //This is the parent of "Users" and "Cases"
        Users: a.b,
        Cases: a.c.d
    },
    withoutparent: $.Object.Users,
    withparent: parent($.Object.Users),
    withparent2: $.Object.Users | parent(@)
}

In the transformation above, the $-operator is used on lines 6-8 to refer to the Object.Users value on line 2, while the @-operator is used on line 8 after the pipe to refer to the current node (which is the Object.Users value).

In the output data, the “Object” object appears first, as it was defined in the transformation and is not part of the sample data, and immediately after, the differences between using and not using the parent function on the same data can be observed.

{
    "Object": {
        "Users": 1,
        "Cases": 2
    },
    "withoutparent": 1,
    "withparent": {
        "Users": 1,
        "Cases": 2
    },
    "withparent2": {
        "Users": 1,
        "Cases": 2
    }
}

It is also possible to go further steps back, and retrieve earlier ancestors of an array, object, or key-value pair by using parent within parent. This is demonstrated in the example below:

{
    "a": {           //This is the parent of "b" & "c" and the grandparent of "d"
        "b": 1,
        "c": {         //This is the child of "a" and parent of "d"
            "d": 2
        }
    }
}

On line 4 of the transformation below, parented data is used within a parent function. This is used to move 2 steps back from the scope. More specifically, the difference between not using parent and using it once or twice is demonstrated using the 3 keys.

{
    withoutparent: a.c.d,
    withparent: parent(a.c.d),
    withgrandparent: parent(parent(a.c.d))
}

The first key (withoutparent), where the parent function is not used, results in the value of “d” (i.e. 2). The result of the second key (withparent) is practically the value of the “c” key from the sample, which is also the parent of “d”, while the third key (withgrandparent) goes 2 steps back from “d” and results in the value of “a” from the sample data, which is practically the parent of “c” and the grandparent of “d”.

{
    "withoutparent": 2,
    "withparent": {        //Practically the value of "c" from the sample data
        "d": 2
    },
    "withgrandparent": {   //Practically the value of "a" from the sample data
        "b": 1,
        "c": {
            "d": 2
        }
    }
}