hmims
hmims

Reputation: 539

Can we insert multiple rows using post api request in Big Query?

I am passing array of 2 JSONs. But only data of first JSON is inserted in Big Query. Can anyone please suggest where am i going wrong?

var req = {
    method: 'POST',
    url: 'https://www.googleapis.com/bigquery/v2/projects/pid/datasets/dataid/tables/tabid/insertAll',
    headers: {
      'Authorization': token1,
      'Content-Type': 'application/json',
      'scope': 'https://www.googleapis.com/auth/bigquery'
    },
    json: {
      "rows": [{
        "json": [{
          'code': 'X-new',
          "remark": '',
          'resulting_status': 'Cancelled'
        }, {
          'code': 'X-jdkdjk',
          "remark": '',
          'resulting_status': 'Required'
        }]
      }]
    }
  };

  console.log(JSON.stringify(req.json.rows));
  request(req, function(error, response, body) {
    if (error)
      debug("Error occurred from client's server" + error);
    else
      console.log("Response......" + JSON.stringify(response.body));
  });

Upvotes: 1

Views: 1189

Answers (1)

Sean Chen
Sean Chen

Reputation: 671

I think your request body is wrong. You have multiple elements in the "json" array field (which should actually just be an object). You should really have multiple elements in the "rows" field. Here's what I think your request should look like:

json: {
      "rows": [{
        // optional insert id here.
        "json": {
          'code': 'X-new',
          "remark": '',
          'resulting_status': 'Cancelled'
        }
      }, {
        // optional insert id here.
        "json": {
          'code': 'X-jdkdjk',
          "remark": '',
          'resulting_status': 'Required'
        }
      }]
    }

Upvotes: 2

Related Questions