0001 ---
0002 layout: global
0003 title: JSON Files
0004 displayTitle: JSON Files
0005 license: |
0006 Licensed to the Apache Software Foundation (ASF) under one or more
0007 contributor license agreements. See the NOTICE file distributed with
0008 this work for additional information regarding copyright ownership.
0009 The ASF licenses this file to You under the Apache License, Version 2.0
0010 (the "License"); you may not use this file except in compliance with
0011 the License. You may obtain a copy of the License at
0012
0013 http://www.apache.org/licenses/LICENSE-2.0
0014
0015 Unless required by applicable law or agreed to in writing, software
0016 distributed under the License is distributed on an "AS IS" BASIS,
0017 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018 See the License for the specific language governing permissions and
0019 limitations under the License.
0020 ---
0021
0022 <div class="codetabs">
0023
0024 <div data-lang="scala" markdown="1">
0025 Spark SQL can automatically infer the schema of a JSON dataset and load it as a `Dataset[Row]`.
0026 This conversion can be done using `SparkSession.read.json()` on either a `Dataset[String]`,
0027 or a JSON file.
0028
0029 Note that the file that is offered as _a json file_ is not a typical JSON file. Each
0030 line must contain a separate, self-contained valid JSON object. For more information, please see
0031 [JSON Lines text format, also called newline-delimited JSON](http://jsonlines.org/).
0032
0033 For a regular multi-line JSON file, set the `multiLine` option to `true`.
0034
0035 {% include_example json_dataset scala/org/apache/spark/examples/sql/SQLDataSourceExample.scala %}
0036 </div>
0037
0038 <div data-lang="java" markdown="1">
0039 Spark SQL can automatically infer the schema of a JSON dataset and load it as a `Dataset<Row>`.
0040 This conversion can be done using `SparkSession.read().json()` on either a `Dataset<String>`,
0041 or a JSON file.
0042
0043 Note that the file that is offered as _a json file_ is not a typical JSON file. Each
0044 line must contain a separate, self-contained valid JSON object. For more information, please see
0045 [JSON Lines text format, also called newline-delimited JSON](http://jsonlines.org/).
0046
0047 For a regular multi-line JSON file, set the `multiLine` option to `true`.
0048
0049 {% include_example json_dataset java/org/apache/spark/examples/sql/JavaSQLDataSourceExample.java %}
0050 </div>
0051
0052 <div data-lang="python" markdown="1">
0053 Spark SQL can automatically infer the schema of a JSON dataset and load it as a DataFrame.
0054 This conversion can be done using `SparkSession.read.json` on a JSON file.
0055
0056 Note that the file that is offered as _a json file_ is not a typical JSON file. Each
0057 line must contain a separate, self-contained valid JSON object. For more information, please see
0058 [JSON Lines text format, also called newline-delimited JSON](http://jsonlines.org/).
0059
0060 For a regular multi-line JSON file, set the `multiLine` parameter to `True`.
0061
0062 {% include_example json_dataset python/sql/datasource.py %}
0063 </div>
0064
0065 <div data-lang="r" markdown="1">
0066 Spark SQL can automatically infer the schema of a JSON dataset and load it as a DataFrame. using
0067 the `read.json()` function, which loads data from a directory of JSON files where each line of the
0068 files is a JSON object.
0069
0070 Note that the file that is offered as _a json file_ is not a typical JSON file. Each
0071 line must contain a separate, self-contained valid JSON object. For more information, please see
0072 [JSON Lines text format, also called newline-delimited JSON](http://jsonlines.org/).
0073
0074 For a regular multi-line JSON file, set a named parameter `multiLine` to `TRUE`.
0075
0076 {% include_example json_dataset r/RSparkSQLExample.R %}
0077
0078 </div>
0079
0080 <div data-lang="SQL" markdown="1">
0081
0082 {% highlight sql %}
0083
0084 CREATE TEMPORARY VIEW jsonTable
0085 USING org.apache.spark.sql.json
0086 OPTIONS (
0087 path "examples/src/main/resources/people.json"
0088 )
0089
0090 SELECT * FROM jsonTable
0091
0092 {% endhighlight %}
0093
0094 </div>
0095
0096 </div>