title: how to create structured JSON from CSV in AWS Glue
body: <p>I am able to create structured json in expected format but extra slashes appear in the JSON records and also json records shows as a string object.</p>
  <p>Please elaborate the solution or let me know what is is missing or if any other approach exists to achieve the expected result.</p>
  <p><strong>My Current Result:</strong></p>
  <pre><code>{
     &quot;awsservices&quot;:[
        &quot;{&quot;\key&quot;:\&quot;string_value&quot;\, \&quot;key&quot;:\numeric_value, &quot;\key&quot;:\&quot;amazon\web/services&quot;}&quot;,
        &quot;{&quot;\key&quot;:\&quot;string_value&quot;\, \&quot;key&quot;:\numeric_value, &quot;\key&quot;:\&quot;amazon\web/services&quot;}&quot;,
        &quot;{&quot;\key&quot;:\&quot;string_value&quot;\, \&quot;key&quot;:\numeric_value, &quot;\key&quot;:\&quot;amazon\web/services&quot;}&quot;,
        &quot;{&quot;\key&quot;:\&quot;string_value&quot;\, \&quot;key&quot;:\numeric_value, &quot;\key&quot;:\&quot;amazon\web/services&quot;}&quot;
     ]
  } 
  </code></pre>
  <p><strong>Expected Result:</strong></p>
  <pre><code>{
     &quot;awsservices&quot;:[
        {&quot;key&quot;:&quot;string_value&quot;, &quot;key&quot;:numeric_value, &quot;key&quot;:&quot;amazon web services&quot;},
        {&quot;key&quot;:&quot;string_value&quot;, &quot;key&quot;:numeric_value, &quot;key&quot;:&quot;amazon web services&quot;},
        {&quot;key&quot;:&quot;string_value&quot;, &quot;key&quot;:numeric_value, &quot;key&quot;:&quot;amazon web services&quot;},
        {&quot;key&quot;:&quot;string_value&quot;, &quot;key&quot;:numeric_value, &quot;key&quot;:&quot;amazon\web/services&quot;}
     ]
  } 
  </code></pre>
  <p><strong>My Code:</strong></p>
  <pre><code>SourceDataDYF = glueContext.create_dynamic_frame.from_options(
     format_options = {&quot;quoteChar&quot;: '&quot;', &quot;escaper&quot;:&quot;&quot;, &quot;withHeader&quot;:True, &quot;separator&quot;:&quot;|&quot;, &quot;inferSchema&quot;:&quot;false&quot;},
     connection_type = &quot;s3&quot;,
     format = &quot;csv&quot;,
     connection_options = {&quot;paths&quot;: &quot;s3:bucket_name/csv_file_path/&quot;], &quot;recurse&quot;:True},
     transformation_ctx = &quot;SourceDataDYF&quot;
  )
  
  StageDataDF = SourceDataDYF.toDF()
  
  print(&quot;*******************************: WRITE JSON :*******************************&quot;)
  
  PreStageDataDF1 = StageDataDF.select(to_json(struct(*StageDataDF.columns)).alias(&quot;json&quot;)) \
     .groupBy(spark_partition_id()) \
     .agg(collect_list(&quot;json&quot;).alias(&quot;awsservices&quot;)) \
     .select(col(&quot;awsservices&quot;).cast(&quot;string&quot;)).coalesce(1)
  
  targetDataDYF = DynamicFrame.fromDF(PreStageDataDF1,glueContext,&quot;PreStageDataDF1&quot;)
  targetDataJSON = glueContext.write_dynamic_frame.from_options(
     frame = targetDataDYF,
     connection_type = &quot;s3&quot;,
     connection_options = {&quot;path&quot;: &quot;s3://result_bucket_name/folder_path/&quot;, &quot;partitionKeys&quot;: []},
     format = &quot;json&quot;,
     transformation_ctx = &quot;targetDataJSON&quot;
  )
  </code></pre>