Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Get value by key from json array

import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._
val df1 = spark.read.format("text").load("file path")
val schema = ArrayType(StructType(Array(
      StructField("key", StringType),
      StructField("value", StringType)
    )))

val arrayToMap = udf[Map[String, String], Seq[Row]] {
  array => array.map { case Row(key: String, value: String) => (key, value) }.toMap
}
val dfJSON = df1.withColumn("jsonData",from_json(col("value"),schema))
    .select("jsonData").withColumn("address", arrayToMap(col("jsonData")))
    .withColumn("city", when(col("address.city").isNotNull, col("address.city")).otherwise(lit(""))).withColumn("street",  when(col("address.street").isNotNull, col("address.street")).otherwise(lit("")))
dfJSON.printSchema()
dfJSON.show(false)
Comment

PREVIOUS NEXT
Code Example
Javascript :: generar numero aleatorio en un rango 
Javascript :: How to access a preexisting collection with Mongoose 
Javascript :: Special Chars like DOTS in Express.js route 
Javascript :: p5 filter 
Javascript :: assign-only-if-property-exists-in-target-object 
Javascript :: TypeError: table.fnFilter is not a function 
Javascript :: morgan 
Javascript :: react text editor snippet 
Javascript :: javascript loop through collection of objects 
Javascript :: clickable image full screen javascript 
Javascript :: online convert javascript to typescript 
Javascript :: create a group or pool in phaser 
Javascript :: Setting Multiples Properties With Array 
Javascript :: Accessing Our CryptoCurrency blockchain through local server 
Javascript :: counter using redux 
Javascript :: ip scanner node 
Javascript :: adding amplify in index.js react native 
Javascript :: lowercase vs lower locale 
Javascript :: Error: Invalid route module file 
Javascript :: How to Solve the Parking Lot Challenge in JavaScript 
Javascript :: json to dart dummy api 
Javascript :: component rerendering when view port comes 
Javascript :: mongoose export collection 
Javascript :: how to render react quill the way it is without the html tags 
Javascript :: quill js laravel 
Javascript :: filter function in javascript 
Javascript :: callback function jquery 
Javascript :: javascript every function 
Javascript :: 2d arrays js 
Javascript :: sort include sequelize 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =