A
A
arsenaljek2021-08-23 07:30:48
Google Apps Script
arsenaljek, 2021-08-23 07:30:48

Google sheets JSON.parse by API. How?

Help understand. I've been fighting for 4 days now and I don't understand.
I have a task. Insert the rest of the goods into the Google spreadsheet by api ozon.
The code

function getBaseUsers() {
  var options = {
    method: "POST",
   "contentType" : "application/json",
   "headers" : {
      "Client-Id": "XXX",
      "Api-Key": "XXX-XXX-XXX",
      //"Content-Type": "application/json"
   },
    "payload": {
    "page": 0,  // Номер страницы, возвращаемой в запросе.
    "page_size": 0 // Количество элементов на странице.
    }
  }
  var response = UrlFetchApp.fetch("https://api-seller.ozon.ru/v2/product/info/stocks", options);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getSheetByName("Лист1"); 

  var dataSet = dataAll.items; 
  
  var rows = [],
    data;
  
  for (i = 0; i < dataSet.length; i++) {
  data = dataSet[i];
  rows.push([data.data.offer_id, data.data.type, data.data.present]);
  }

  dataRange = sheet.getRange(2, 1, rows.length, 3);
  dataRange.setValues(rows);

}

I'm testing in their documentation https://docs.ozon.ru/api/seller/#operation/Product... everything works.
{
    "page": 0,
    "page_size": 0
}

How to do it in google spreadsheet?
Now I am getting this error
Ошибка
Exception: Request failed for https://api-seller.ozon.ru returned code 400. Truncated server response: {"code":3, "message":"invalid character 'p' looking for beginning of value", "details":[]} (use muteHttpExceptions option to examine full response)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
inFureal, 2021-08-23
@arsenaljek

As written in the documentation

// Make a POST request with a JSON payload.
var data = {
  'name': 'Bob Smith',
  'age': 35,
  'pets': ['fido', 'fluffy']
};
var options = {
  'method' : 'post',
  'contentType': 'application/json',
  // Convert the JavaScript object to a JSON string.
  'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('https://httpbin.org/post', options);

Those. options.payload wrapped in JSON.stingify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question