--- layout: default title: JSON Support parent: SQL nav_order: 7 --- # JSON Support SQL plugin supports JSON by following [PartiQL](https://partiql.org/) specification, a SQL-compatible query language that lets you query semi-structured and nested data for any data format. The SQL plugin only supports a subset of the PartiQL specification. ## Querying nested collection PartiQL extends SQL to allow you to query and unnest nested collections. In OpenSearch, this is very useful to query a JSON index with nested objects or fields. To follow along, use the `bulk` operation to index some sample data: ```json POST employees_nested/_bulk?refresh {"index":{"_id":"1"}} {"id":3,"name":"Bob Smith","title":null,"projects":[{"name":"SQL Spectrum querying","started_year":1990},{"name":"SQL security","started_year":1999},{"name":"OpenSearch security","started_year":2015}]} {"index":{"_id":"2"}} {"id":4,"name":"Susan Smith","title":"Dev Mgr","projects":[]} {"index":{"_id":"3"}} {"id":6,"name":"Jane Smith","title":"Software Eng 2","projects":[{"name":"SQL security","started_year":1998},{"name":"Hello security","started_year":2015,"address":[{"city":"Dallas","state":"TX"}]}]} ``` ### Example 1: Unnesting a nested collection This example finds the nested document (`projects`) with a field value (`name`) that satisfies the predicate (contains `security`). Because each parent document can have more than one nested documents, the nested document that matches is flattened. In other words, the final result is the cartesian product between the parent and nested documents. ```sql SELECT e.name AS employeeName, p.name AS projectName FROM employees_nested AS e, e.projects AS p WHERE p.name LIKE '%security%' ``` Explain: ```json { "from" : 0, "size" : 200, "query" : { "bool" : { "filter" : [ { "bool" : { "must" : [ { "nested" : { "query" : { "wildcard" : { "projects.name" : { "wildcard" : "*security*", "boost" : 1.0 } } }, "path" : "projects", "ignore_unmapped" : false, "score_mode" : "none", "boost" : 1.0, "inner_hits" : { "ignore_unmapped" : false, "from" : 0, "size" : 3, "version" : false, "seq_no_primary_term" : false, "explain" : false, "track_scores" : false, "_source" : { "includes" : [ "projects.name" ], "excludes" : [ ] } } } } ], "adjust_pure_negative" : true, "boost" : 1.0 } } ], "adjust_pure_negative" : true, "boost" : 1.0 } }, "_source" : { "includes" : [ "name" ], "excludes" : [ ] } } ``` Result set: | employeeName | projectName :--- | :--- Bob Smith | OpenSearch Security Bob Smith | SQL security Jane Smith | Hello security Jane Smith | SQL security ### Example 2: Unnesting in existential subquery To unnest a nested collection in a subquery to check if it satisfies a condition: ```sql SELECT e.name AS employeeName FROM employees_nested AS e WHERE EXISTS ( SELECT * FROM e.projects AS p WHERE p.name LIKE '%security%' ) ``` Explain: ```json { "from" : 0, "size" : 200, "query" : { "bool" : { "filter" : [ { "bool" : { "must" : [ { "nested" : { "query" : { "bool" : { "must" : [ { "bool" : { "must" : [ { "bool" : { "must_not" : [ { "bool" : { "must_not" : [ { "exists" : { "field" : "projects", "boost" : 1.0 } } ], "adjust_pure_negative" : true, "boost" : 1.0 } } ], "adjust_pure_negative" : true, "boost" : 1.0 } }, { "wildcard" : { "projects.name" : { "wildcard" : "*security*", "boost" : 1.0 } } } ], "adjust_pure_negative" : true, "boost" : 1.0 } } ], "adjust_pure_negative" : true, "boost" : 1.0 } }, "path" : "projects", "ignore_unmapped" : false, "score_mode" : "none", "boost" : 1.0 } } ], "adjust_pure_negative" : true, "boost" : 1.0 } } ], "adjust_pure_negative" : true, "boost" : 1.0 } }, "_source" : { "includes" : [ "name" ], "excludes" : [ ] } } ``` Result set: | employeeName | :--- | :--- Bob Smith | Jane Smith |