title: Reading JSON Array from Storage and send to GCP PubSub
body: <p>I am having multiple files in Google Cloud Storage, each file containing JSON array like below - </p>
  
  <pre><code>{
    "Data": [
      {
        "Country": "IN",
        "Order": "1033616591",
        "Method": "LCD zone E same day",
        "WorkOrderNo": "1033616591",
        "Zipcode": "6020",
        "OriginalTimeSlot": "2019-05-29 14:00-18:00",
        "CurrentTimeSlot": "2019-05-29 14:00-18:00",
        "Shipment": "98:2",
        "WOCreationDate": "2019-05-27T18:21:15Z",
        "ModifactionDate": "2020-01-17T16:50:58Z",
        "Dispatch": {
          "Status": "00",
          "DispatchUnit": []
        },
        "Parcels": {
          "Parcel": [
            {
              "Active": true,
              "Weight": 29.087833333333332,
              "Volume": 0.27791899999999997,
              "Trackingstatus": "",
              "Number": "704648048"
            },
            {
              "Active": true,
              "Weight": 29.087833333333332,
              "Volume": 0.27791899999999997,
              "Trackingstatus": "",
              "Number": "704648049"
            },
            {
              "Active": true,
              "Weight": 29.087833333333332,
              "Volume": 0.27791899999999997,
              "Trackingstatus": "",
              "Number": "704648050"
            },
            {
              "Active": true,
              "Weight": 29.087833333333332,
              "Volume": 0.27791899999999997,
              "Trackingstatus": "",
              "Number": "704648051"
            },
            {
              "Active": true,
              "Weight": 29.087833333333332,
              "Volume": 0.27791899999999997,
              "Trackingstatus": "",
              "Number": "704648052"
            },
            {
              "Active": true,
              "Weight": 29.087833333333332,
              "Volume": 0.27791899999999997,
              "Trackingstatus": "",
              "Number": "704648053"
            }
          ]
        },
        "TimeSlotId": "d2916acd-1f36-4604-98dc-0d11014a045c"
      },
      {
        "Country": "IN",
        "Order": "1049968941",
        "Method": "LCD zone A",
        "WorkOrderNo": "1049968941",
        "Zipcode": "6020",
        "OriginalTimeSlot": "2019-09-26 06:00-10:00",
        "CurrentTimeSlot": "2019-09-26 06:00-10:00",
        "Shipment": "98:2",
        "WOCreationDate": "2019-09-02T16:17:13Z",
        "ModifactionDate": "2020-01-17T16:40:18Z",
        "Dispatch": {
          "Status": "00",
          "DispatchUnit": []
        },
        "Parcels": {
          "Parcel": [
            {
              "Active": true,
              "Weight": 44.5,
              "Volume": 1.147163,
              "Trackingstatus": "",
              "Number": "704987779"
            }
          ]
        },
        "TimeSlotId": "3c3da1d2-000d-402a-856d-0d89013a6961"
      }
    ]
  }
  </code></pre>
  
  <p>Now I am trying to read each file from storage, separate each JSON based on "Country" field and publish the same to Google PubSub. Below what I tried - </p>
  
  <pre><code>const express = require('express')
  const app = express()
  const port = 8080
  const { PubSub } = require('@google-cloud/pubsub');
  
  const projectId = 'my_project_id';
  const keyFilename = 'myjson.json';
  const pubsub = new PubSub({ projectId, keyFilename });
  const topicName = 'pubsub_topic_name';
  const subscriptionName = 'pubsub_subscription_name';
  const { Storage } = require('@google-cloud/storage');
  const storage = new Storage();
  const timeout = 60;
  
  const subscription = pubsub.subscription(subscriptionName);
  let messageCount = 0;
  const bucketName = 'temp-shipment';
  
  app.get('/', async function (req, res) {
      var messageIds = "";
      console.log('Line 1');
      const [files] = await storage.bucket('bucketname').getFiles();
      console.log('Line 2');
      var bkt = '';
      var i = 0;
      files.forEach(file =&gt; {
          console.log('name of file' + file.name);
          var archivo = file.createReadStream();
          bkt = '';
          console.log('---- bkt value:' + bkt);
          archivo.on('data', async function (d) {
              console.log('---- bkt value 2:' + bkt);
              bkt += d;
              i = i + 1;
              console.log('---- bkt value 3:' + bkt);
          }).on('end', async function () {
  
              console.log('---- bkt value 4:' + bkt);
              console.log("&gt;&gt;&gt;&gt;END CALLED i" + i)
              console.log("bky:" + bkt.replace(/(?:\\[rn])+/g, '').trim().toString());
  
              try {
                  var kktrim = bkt.replace(/(?:\\[rn])+/g, '').trim();
                  var kk = JSON.parse(kktrim);
  
                  for (var v of kk.Data) {
  
                      var myJsonObject = { message: JSON.stringify(v) }
                      const data = JSON.stringify(myJsonObject);
                      console.log("Data: " + data);
                      const dataBuffer = Buffer.from(data);
                     // console.log("buffer" + dataBuffer);
                      const messageId = await pubsub.topic(topicName).publish(dataBuffer);
                      console.log("MessageId&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;" + messageId);
                      //messageIds += ":" + messageId;
                      //console.log(messageIds);
  
                  }
                  bkt = '';
              } catch (ex) {
                  console.log('error' + ex + 'in ' + file.name);
              }
  
  
          })
  
  
      });
  
      res.send('Message successfully sent!\nTopic: messages' + messageIds);
  });
  app.listen(port, () =&gt; console.log(`Example app listening on port ${port}!`))
  </code></pre>
  
  <p>This code reading the files, but the code actually converting one file and giving error for others. For other files, its throwing this error - </p>
  
  <pre><code>textPayload: "errorSyntaxError: Unexpected token { in JSON at position 1696in IN.json" 
  </code></pre>
  
  <p>Need some help to fix the issue. I am unable to understand what i am missing here.</p>