Free-Form Queries
As discussed in Automatic Schema Discovery, intuited table schemas enable SQL access to unstructured JSON data. Customizing Schemas enables you to define static tables and gives you more granular control over the relational view of your data; for example, you can change the data types reported. However, you are not limited to the schema's view of your data.
You can query any nested structure without flattening the data. Any relations that you can access through Automatic Schema Discovery can also be accessed with an ad hoc SQL query.
Extended Projection Syntax
In the SELECT clause, use dot notation to specify an XPath to the data, as in the following query.
SELECT [personal.name.last], [personal.name.first], [vehicles.1.type], [vehicles.1.model] FROM people WHERE [personal.name.last] = 'Roberts' AND [personal.name.first] = 'Jane'
Note that to specify the path to a specific array element, specify the element's ordinal position. Arrays have a zero-based index, so the preceding query retrieves the second vehicle.
Example
The preceding query draws the column names from the example people document in Raw Data. Below is a person object from the array of people:
{
"people": [
{
"personal": {
"age": 24,
"gender": "F",
"name": {
"first": "Jane",
"last": "Roberts"
}
},
"vehicles": [
{
"type": "car",
"model": "Toyota Camry",
"insurance": {
"company": "Car Insurance",
"policy_num": "98765"
},
"maintenance": [
{
"date": "05-11-2017",
"desc": "tires rotated"
},
{
"date": "11-03-2017",
"desc": "oil change"
}
]
},
{
"type": "car",
"model": "Honda Accord",
"insurance": {
"company": "Car Insurance",
"policy_num": "98765"
},
"maintenance": [
{
"date": "10-07-2017",
"desc": "new air filter"
},
{
"date": "01-13-2018",
"desc": "new brakes"
}
]
}
],
"source": "phone"
}
]
}
Connection String
With the following connection string, the provider will not parse nested data -- the data is processed when you execute the query. The properties of the top-level object are still flattened through the default FlattenObjects functionality. Nested data is returned as a JSON aggregate.
URI=C:\people.txt;DataModel=Document;JSONPath='$.people;'
Query
You can access any nested structure in the Raw Data document as a column:
SELECT [personal.name.last], [personal.name.first], [vehicles.1.type], [vehicles.1.model] FROM people WHERE [personal.name.last] = 'Roberts' AND [personal.name.first] = 'Jane'
Note that arrays have a zero-based index. For example, the following query retrieves the person's second vehicle in the example:
Results
The preceding query returns the following results:
Column Name | Data Type | Example Value |
personal.name.first | String | Jane |
personal.name.last | String | Roberts |
vehicles.1.type | String | car |
vehicles.1.model | String | Honda Accord |