This is an automated email from the ASF dual-hosted git repository. srowen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push: new 9e9fe6f9ea9 [SPARK-39982][DOC] Add doc string to StructType.fromJson 9e9fe6f9ea9 is described below commit 9e9fe6f9ea92eb1b8dde8854f01147b68af4b8d4 Author: Khalid Mammadov <khalidmammad...@gmail.com> AuthorDate: Mon Aug 15 08:31:12 2022 -0500 [SPARK-39982][DOC] Add doc string to StructType.fromJson ### What changes were proposed in this pull request? Documentation provided for StructType.fromJson method ### Why are the changes needed? To make it easy for a user to understand this method and use it. It was inspired by [this SO question](https://stackoverflow.com/questions/73233593/pyspark-typeerror-col-should-be-column/73235143) ### Does this PR introduce _any_ user-facing change? Yes, adding missing documentation ### How was this patch tested? Unit tests Closes #37408 from khalidmammadov/add_docstr_to_from_json. Authored-by: Khalid Mammadov <khalidmammad...@gmail.com> Signed-off-by: Sean Owen <sro...@gmail.com> --- python/pyspark/sql/types.py | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py index 2b4a6713890..8255aca8f52 100644 --- a/python/pyspark/sql/types.py +++ b/python/pyspark/sql/types.py @@ -856,6 +856,97 @@ class StructType(DataType): @classmethod def fromJson(cls, json: Dict[str, Any]) -> "StructType": + """ + Constructs :class:`StructType` from a schema defined in JSON format. + + Below is a JSON schema it must adhere to:: + + { + "title":"StructType", + "description":"Schema of StructType in json format", + "type":"object", + "properties":{ + "fields":{ + "description":"Array of struct fields", + "type":"array", + "items":{ + "type":"object", + "properties":{ + "name":{ + "description":"Name of the field", + "type":"string" + }, + "type":{ + "description": "Type of the field. Can either be + another nested StructType or primitive type", + "type":"object/string" + }, + "nullable":{ + "description":"If nulls are allowed", + "type":"boolean" + }, + "metadata":{ + "description":"Additional metadata to supply", + "type":"object" + }, + "required":[ + "name", + "type", + "nullable", + "metadata" + ] + } + } + } + } + } + + Parameters + ---------- + json : dict or a dict-like object e.g. JSON object + This "dict" must have "fields" key that returns an array of fields + each of which must have specific keys (name, type, nullable, metadata). + + Returns + ------- + :class:`StructType` + + Examples + -------- + >>> json_str = ''' + ... { + ... "fields": [ + ... { + ... "metadata": {}, + ... "name": "Person", + ... "nullable": true, + ... "type": { + ... "fields": [ + ... { + ... "metadata": {}, + ... "name": "name", + ... "nullable": false, + ... "type": "string" + ... }, + ... { + ... "metadata": {}, + ... "name": "surname", + ... "nullable": false, + ... "type": "string" + ... } + ... ], + ... "type": "struct" + ... } + ... } + ... ], + ... "type": "struct" + ... } + ... ''' + >>> import json + >>> scheme = StructType.fromJson(json.loads(json_str)) + >>> scheme.simpleString() + 'struct<Person:struct<name:string,surname:string>>' + """ return StructType([StructField.fromJson(f) for f in json["fields"]]) def fieldNames(self) -> List[str]: --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org