cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

sample

NathanSundarara
Contributor

Help parsing the JSON using Spark SQL or python. Sample json attached.

4 REPLIES 4

Anonymous
Not applicable

@Nathan Sundararajan​ :

Sure! Here is an example of how to parse the JSON data using Python:

import json
 
# Load the JSON data from a file or string
data = {...}  # Your JSON data here
 
# Parse the JSON data into a Python object
obj = json.loads(data)
 
# Access the data using Python syntax
message_timestamp = obj["messageTimestamp"]
site_reference = obj["siteReference"]
update_count = obj["updateCount"]
updates = obj["updates"]
 
# Iterate through the updates array and access nested data
for update in updates:
    event_timestamp = update["eventTimestamp"]
    spot_reference = update["spotReference"]
    spot_info = update["spotInfo"]
    vehicle_spot = spot_info["vehicleSpot"]
    vehicle_data = spot_info["vehicleData"]
    # Access more nested data as needed...

Alternatively, you can also use Spark SQL to parse the JSON data. Here is an example using the

spark.read.json() method:

from pyspark.sql import SparkSession
 
# Create a SparkSession object
spark = SparkSession.builder.appName("JSON Parsing").getOrCreate()
 
# Load the JSON data from a file or string
data = {...}  # Your JSON data here
 
# Create a DataFrame from the JSON data
df = spark.read.json(sc.parallelize([data]))
 
# Register the DataFrame as a temporary view
df.createOrReplaceTempView("json_data")
 
# Query the data using Spark SQL syntax
result = spark.sql("""
    SELECT messageTimestamp, siteReference, updateCount, updates.eventTimestamp, updates.spotReference,
           updates.spotInfo.vehicleSpot.externalReference, updates.spotInfo.vehicleSpot.state,
           updates.spotInfo.vehicleData.externalReference, updates.spotInfo.vehicleData.state
    FROM json_data
    LATERAL VIEW EXPLODE(updates) AS updates
""")
 
# Show the result
result.show()

This query selects some of the fields from the JSON data and explodes the updates array into separate rows. You can modify the query as needed to access more or less data.

Can you explain how to go all the way to one of the array.. c3fieldstrings inside the field structure please?

"fields": {

"c3FieldStrings": [

{

"externalReference": "4095",

"name": "Blocking Spot"

},

{

"externalReference": "Disabled",

"name": "Disabled",

"value": "false"

},

{

"externalReference": "WorkflowVehicleSpot",

"name": "Spot",

"value": "AA18_FL5"

},

{

"externalReference": "WorkflowVMT",

"name": "WorkflowVMT"

},

NathanSundarara
Contributor

@Suteja Kanuri​  can you please respond to my question above?

Anonymous
Not applicable

@Nathan Sundararajan​ : Does the below help?

data = {
    "fields": {
        "c3FieldStrings": [
            {
                "externalReference": "4095",
                "name": "Blocking Spot"
            },
            {
                "externalReference": "Disabled",
                "name": "Disabled",
                "value": "false"
            },
            {
                "externalReference": "WorkflowVehicleSpot",
                "name": "Spot",
                "value": "AA18_FL5"
            },
            {
                "externalReference": "WorkflowVMT",
                "name": "WorkflowVMT"
            }
        ]
    }
}
 
c3FieldStrings = data["fields"]["c3FieldStrings"]

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group