Search code examples
phpmysqljsonmysql-8.0document-store

MYSQL JSON - how do I access nested JSON object that's indexed by numerical key?


I am trying to extract JSON document subtree that's indexed by numerical key.

My JSON string:

{
    "pk": 20,
    "tree": {
        "100": {
            "values": [
                1, 2, 3
            ]
        },
        "abc" => 999
    }
}

My code:

$session = mysql_xdevapi\getSession("mysqlx://root:letmein@localhost");
$schema = $session->getSchema('test');
$coll = $schema->getCollection('myColl');
$expr = mysql_xdevapi\Expression('$.tree.*');
$result = $coll->find('$.pk=20')->fields(['$.tree[100]'])->execute();

Using '$.tree[100]' results in

    [
        'tree' => null
    ]

Using '$.tree.*' results in

    [
        'tree' => [
            0 => [
                1, 2, 3
            ],
            1 => 999
        ]
    ]

Using '$.tree.abc' results in

    [
        'tree' => [
            'abc' => 999
        ]
    ]

So, '$.tree.abc' works, but '$.tree[100]' doesnt.

Question. How can I access values key using '$.tree[100]' expression?

Thanks!


Solution

  • Thnx for report, following case:

    $expr = mysql_xdevapi\Expression('$.tree."100"'); 
    $result = $coll->find('$.pk=25')->fields($expr)->execute();
    

    will be supported in mysql_xdevapi v8.0.18 which is planned for Oct 14.