2

Say I have the following stored in a JSON column in an Oracle database:

{
    "key-with-hyphens": "value"
}

Assuming this column is called json_column in a table called my_table, then I'm trying to select the value for the "key-with-hyphens" key like so:

select json_column.key-with-hyphens from my_table;

This gives me the following error:

ORA-00936: missing expression
00936. 00000 -  "missing expression"

Is there some kind of special syntax you need to use when selecting the value for a hyphenated key from a JSON column in Oracle?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102

1 Answers1

6

So it turns out that there are a couple ways you can do this.

Method 1:

Doing it the original way I was trying, there were a couple issues with this select:

select json_column.key-with-hyphens from my_table;

First off, key selection in JSON columns will not work unless you alias the table, as such:

select mt.json_column.key-with-hyphens from my_table mt;

However, this still doesn't fix the issue with the hyphenated key. To allow for hyphenated keys in this syntax, you need to wrap the name of the key in quotes:

select mt.json_column."key-with-hyphens" from my_table mt;

The above query will work as expected.

Method 2:

Another way you can do this, without aliasing the table, is using the json_value function in the select clause. You pass the JSON column as the first parameter, and the full path of the key you would like to select as the second parameter, using $ as the root context of the JSON object stored in your column. The following query should also work as expected:

select json_value(json_column, '$."key-with-hyphens"') from my_table;
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102