title: How split one json data into 2 file in cloud function using python
body: <p>The need is to create 2 files in cloud storage bucket using the json data available in pubsub.</p>
  <pre><code>{&quot;ltime&quot;:&quot;2022-04-12T11:33:00.970Z&quot;,&quot;cnt&quot;:199,&quot;fname&quot;:&quot;MYFILENAME&quot;,&quot;data&quot;:[{&quot;NAME&quot;:&quot;N1&quot;,&quot;ID&quot;:11.4,&quot;DATE&quot;:&quot;2005-10-14 00:00:00&quot;},{&quot;NAME&quot;:&quot;M1&quot;,&quot;ID&quot;:25.0,&quot;DATE&quot;:&quot;2005-10-14 00:00:00&quot;}]
  </code></pre>
  <p>Expected is:</p>
  <ol>
  <li><p>Create MYFILENAME.json and then write data values - [{&quot;NAME&quot;:&quot;N1&quot;,&quot;ID&quot;:11.4,&quot;DATE&quot;:&quot;2005-10-14 00:00:00&quot;},{&quot;NAME&quot;:&quot;M1&quot;,&quot;ID&quot;:25.0,&quot;DATE&quot;:&quot;2005-10-14 00:00:00&quot;}]</p>
  </li>
  <li><p>Create MYFILENAME.csv file and then write ltime:2022-04-12T11:33:00.970Z, cnt:199
  (comma separated file and without double quotes)</p>
  </li>
  </ol>
  <p>As per my need I have written Python code to use cloud functions, but it may serves one way not both.</p>
  <pre><code>def pubsub_to_gcs(event, context):
      pubsub_message = base64.b64decode(event['data']).decode('utf-8')
      data = json.loads(pubsub_message)
      jsonfile = data['filename'] + '.json'
      try:
          to_gcs(os.environ['project'], os.environ['bucket'], jsonfile, pubsub_message)
      except Exception as e:
          print('Error: {}'.format(e))
  
  def to_gcs(project_id, bucket, jsonfile, document):
       storage_client = storage.Client(gcs_project_id)
       bucket = storage_client.get_bucket(gcs_bucket_name)
       blob = bucket.blob(jsonfile)
       blob.upload_from_string(data=document, content_type='application/json')
  </code></pre>
  <p>Ultimately my bucket should look like this with above data added:</p>
  <pre><code>my-bucket
     MYFILENAME.json
     MYFILENAME.csv
  </code></pre>
  <p>Please let me know how to handle both ?</p>