Parse json as struct

Supported in: Batch, Streaming

Parses json strings following the given schema definition, ignoring any fields not in the schema.

Expression categories: Data preparation, File, Popular, Struct

Declared arguments

  • Json - Json to be parsed using the schema.
    Expression<String>
  • Schema - Schema definition used when parsing the json strings.
    Type<Array<AnyType> | Map<String, String> | Struct>

Output type: Array<AnyType> | Map<String, String> | Struct

Examples

Example 1: Base case

Argument values:

  • Json: json
  • Schema: Struct<airline, airport<id, miles>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 2000
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 2000,
},
}

Example 2: Null case

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

  • Json: json
  • Schema: Struct<airline, airport<id, miles>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK"
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: null,
},
}

Example 3: Null case

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

  • Json: json
  • Schema: Struct<airline, airport<id, miles>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": null
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: null,
},
}

Example 4: Null case

Description: Test field of struct being an array. Argument values:

  • Json: json
  • Schema: Struct<airline, airport<id, countries<String>>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: JFK,
},
}

Example 5: Null case

Description: Test field of struct being empty string. Argument values:

  • Json: json
  • Schema: Struct<airline, airport<id, countries<String>>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "",
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: empty string,
},
}

Example 6: Null case

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

  • Json: json
  • Schema: Struct<airline, airport<id, countries<String>>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": ["USA", null]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, null ],
id: JFK,
},
}

Example 7: Null case

Description: Test field of struct being a null string. Argument values:

  • Json: json
  • Schema: Struct<airline, airport<id, countries<String>>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": null,
  "countries": ["USA", "Canada"]
 }
}
{
airline: XB-112,
airport: {
countries: [ USA, Canada ],
id: null,
},
}

Example 8: Null case

Description: Test struct with one field being a map. Argument values:

  • Json: json
  • Schema: Struct<airline, airport<id, countries<String, Integer>>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": {"USA": 4}
 }
}
{
airline: XB-112,
airport: {
countries: {
 USA -> 4,
},
id: JFK,
},
}

Example 9: Null case

Description: Parse struct with double field. Argument values:

  • Json: json
  • Schema: Struct<airline, airport<id, miles>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 4.2
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 4.2,
},
}

Example 10: Null case

Description: Ints parsed as doubles should return doubles. Argument values:

  • Json: json
  • Schema: Struct<airline, airport<id, miles>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "miles": 4
 }
}
{
airline: XB-112,
airport: {
id: JFK,
miles: 4.0,
},
}

Example 11: Null case

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

  • Json: json
  • Schema: Struct<airline, airport<id, countries<String, Integer>>>
jsonOutput
{
 "airline": "XB-112",
 "airport": {
  "id": "JFK",
  "countries": {"USA": null}
 }
}
{
airline: XB-112,
airport: {
countries: {
 USA -> null,
},
id: JFK,
},
}