跳转至

Parse JSON string(解析 JSON 字符串)

Supported in: Batch, Faster, Streaming

Parses JSON string following the given schema definition, ignoring any fields not in the schema.

Expression categories: Data preparation, Popular, String, Struct

Declared arguments

  • JSON: JSON to be parsed using the schema.
    Expression\
  • Schema: Schema definition used when parsing the JSON strings.
    Type\ | Map\\ | Struct>
  • optional Output mode: The 'simple' output mode will treat fields that fail to parse as null. The 'with errors' output mode will return a parsable struct with any errors found during parsing in an 'error' field and a valid parsed json in the 'ok' field.
    Enum\

Output type: Array\ | Map\ | Struct

Examples

Example 1: Base case

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 2000
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 2000,
},
}

Example 2: Base case

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: WITH_ERRORS
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 2000
 }
}
{
error: null,
ok: {
airline: XB-112,
airport: {
id: JFK,
miles: 2000,
},
},
}

Example 3: Null case

Description: When a requested field is missing in the input JSON the field becomes null.

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK"
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: null,
},
}

Example 4: Null case

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: null
json Output
null null

Example 5: Null case

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: WITH_ERRORS
json Output
null {
error: JSON input is null or empty,
ok: null,
}

Example 6: Null case

Description: When a requested field is null in the input JSON the field becomes null.

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": null
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: null,
},
}

Example 7: Null case

Description: Test field of struct being an array.

Argument values:

  • JSON: json
  • Schema: Struct\>>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: JFK,
},
}

Example 8: Null case

Description: Test field of struct being empty string.

Argument values:

  • JSON: json
  • Schema: Struct\>>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "",
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: empty string,
},
}

Example 9: Null case

Description: Test field of struct being an array with null element.

Argument values:

  • JSON: json
  • Schema: Struct\>>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": ["USA", null]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, null ],
id: JFK,
},
}

Example 10: Null case

Description: Test field of struct being a null string.

Argument values:

  • JSON: json
  • Schema: Struct\>>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": null,
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: null,
},
}

Example 11: Null case

Description: Test struct with one field being a map.

Argument values:

  • JSON: json
  • Schema: Struct\>>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": {"USA": 4}
 }
}
{
airline: XB-112,
airport: {
countries: {
 USA -> 4,
},
id: JFK,
},
}

Example 12: Null case

Description: Parse struct with double field.

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 4.2
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 4.2,
},
}

Example 13: Null case

Description: Ints parsed as doubles should return doubles.

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 4
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 4.0,
},
}

Example 14: Null case

Description: When a map has a null value, the resultant struct will have a null value.

Argument values:

  • JSON: json
  • Schema: Struct\>>
  • Output mode: null
json Output
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": {"USA": null}
 }
}
{
airline: XB-112,
airport: {
countries: {
 USA -> null,
},
id: JFK,
},
}

Example 15: Edge case

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: WITH_ERRORS
json Output
invalid {
error: The JSON content is invalid or malformed,
ok: null,
}

Example 16: Edge case

Argument values:

  • JSON: json
  • Schema: Struct\, myArray:Array\\>
  • Output mode: WITH_ERRORS
json Output
{
 "timestampVal": "This is a string."
}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"boolVal": 5}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"byteVal": "This is not a byte."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"shortVal": "This is not a short."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"longVal": "This is not a long."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"intVal": 5.2}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"doubleVal": "This is not a double."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"floatVal": "This is not a float."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"dateVal": "32/13/2020"}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"decimalVal": "This is not a decimal."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"myMap": {"a": "str"}}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"myArray": ["a", "b"]}
{
error: The JSON content does not match the expected type,
ok: null,
}

Example 17: Edge case

Argument values:

  • JSON: json
  • Schema: Struct\, boolVal:Boolean>
  • Output mode: WITH_ERRORS
json Output
{
 "boolVal": true
}
{
error: null,
ok: {
airport: null,
boolVal: true,
},
}
{
 "boolVal": "This is a string."
}
{
error: The JSON content does not match the expected type,
ok: null,
}

Example 18: Edge case

Argument values:

  • JSON: json
  • Schema: Struct\>
  • Output mode: WITH_ERRORS
json Output
{
 "arrival_time":
{
error: There was an unexpected EOF while parsing the JSON,
ok: null,
}


中文翻译


解析 JSON 字符串

支持:批处理(Batch)、快速处理(Faster)、流处理(Streaming)

根据给定的模式定义解析 JSON 字符串,忽略模式中未包含的任何字段。

表达式类别: 数据准备、常用、字符串、结构体(Struct)

声明的参数

  • JSON: 要使用模式进行解析的 JSON。
    表达式\
  • 模式(Schema): 解析 JSON 字符串时使用的模式定义。
    类型\ | Map\\ | Struct>
  • 可选 输出模式(Output mode): 'simple' 输出模式会将解析失败的字段视为 null。'with errors' 输出模式会返回一个可解析的结构体,其中包含解析过程中发现的任何错误(位于 'error' 字段)以及有效的已解析 JSON(位于 'ok' 字段)。
    枚举\

输出类型: Array\ | Map\ | Struct

示例

示例 1:基本情况

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 2000
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 2000,
},
}

示例 2:基本情况

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: WITH_ERRORS
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 2000
 }
}
{
error: null,
ok: {
airline: XB-112,
airport: {
id: JFK,
miles: 2000,
},
},
}

示例 3:空值情况

描述: 当输入 JSON 中缺少请求的字段时,该字段变为 null。

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK"
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: null,
},
}

示例 4:空值情况

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: null
json 输出
null null

示例 5:空值情况

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: WITH_ERRORS
json 输出
null {
error: JSON input is null or empty,
ok: null,
}

示例 6:空值情况

描述: 当输入 JSON 中请求的字段为 null 时,该字段变为 null。

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": null
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: null,
},
}

示例 7:空值情况

描述: 测试结构体字段为数组的情况。

参数值:

  • JSON: json
  • 模式: Struct\>>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: JFK,
},
}

示例 8:空值情况

描述: 测试结构体字段为空字符串的情况。

参数值:

  • JSON: json
  • 模式: Struct\>>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "",
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: empty string,
},
}

示例 9:空值情况

描述: 测试结构体字段为包含 null 元素的数组的情况。

参数值:

  • JSON: json
  • 模式: Struct\>>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": ["USA", null]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, null ],
id: JFK,
},
}

示例 10:空值情况

描述: 测试结构体字段为 null 字符串的情况。

参数值:

  • JSON: json
  • 模式: Struct\>>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": null,
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: null,
},
}

示例 11:空值情况

描述: 测试其中一个字段为映射(Map)的结构体。

参数值:

  • JSON: json
  • 模式: Struct\>>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": {"USA": 4}
 }
}
{
airline: XB-112,
airport: {
countries: {
 USA -> 4,
},
id: JFK,
},
}

示例 12:空值情况

描述: 解析包含双精度浮点型(Double)字段的结构体。

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 4.2
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 4.2,
},
}

示例 13:空值情况

描述: 解析为双精度浮点型的整数应返回双精度浮点型。

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 4
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 4.0,
},
}

示例 14:空值情况

描述: 当映射包含 null 值时,生成的结构体将包含 null 值。

参数值:

  • JSON: json
  • 模式: Struct\>>
  • 输出模式: null
json 输出
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": {"USA": null}
 }
}
{
airline: XB-112,
airport: {
countries: {
 USA -> null,
},
id: JFK,
},
}

示例 15:边界情况

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: WITH_ERRORS
json 输出
invalid {
error: The JSON content is invalid or malformed,
ok: null,
}

示例 16:边界情况

参数值:

  • JSON: json
  • 模式: Struct\, myArray:Array\\>
  • 输出模式: WITH_ERRORS
json 输出
{
 "timestampVal": "This is a string."
}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"boolVal": 5}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"byteVal": "This is not a byte."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"shortVal": "This is not a short."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"longVal": "This is not a long."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"intVal": 5.2}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"doubleVal": "This is not a double."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"floatVal": "This is not a float."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"dateVal": "32/13/2020"}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"decimalVal": "This is not a decimal."}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"myMap": {"a": "str"}}
{
error: The JSON content does not match the expected type,
ok: null,
}
{"myArray": ["a", "b"]}
{
error: The JSON content does not match the expected type,
ok: null,
}

示例 17:边界情况

参数值:

  • JSON: json
  • 模式: Struct\, boolVal:Boolean>
  • 输出模式: WITH_ERRORS
json 输出
{
 "boolVal": true
}
{
error: null,
ok: {
airport: null,
boolVal: true,
},
}
{
 "boolVal": "This is a string."
}
{
error: The JSON content does not match the expected type,
ok: null,
}

示例 18:边界情况

参数值:

  • JSON: json
  • 模式: Struct\>
  • 输出模式: WITH_ERRORS
json 输出
{
 "arrival_time":
{
error: There was an unexpected EOF while parsing the JSON,
ok: null,
}