0

I'm trying to import JSON data to Google Sheets, but it imports JSON data as HTML.

my formulas are:
=INDEX(IMPORTDATA("https://instagram.com/"&B1&"/?__a=1"),1,1) - where B1 is username
=VALUE(REGEXREPLACE(INDEX(IMPORTDATA("https://instagram.com/"&B1&"/?__a=1"),1,1),"[^[:digit:]]", "")) << this formula is pulling numbers from a string.

Sometimes it works and sometimes gives only "0".
When I try to open this link with chrome:
looks like raw JSON

I tried another approach with the Google apps script, but it has the same problem: SyntaxError: Unexpected token < in JSON at position 0 (I suppose it gets "<" from HTML) I tried to stringify, but HTML has a different content, there is no instagram ID info. Please, help me how to get this data from JSON to Google Sheets: "logging_page_id":"profilePage_460563723"

function getID() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("insta");
var user_name = sheet.getRange("B1").getValue();
var IDcell = sheet.getRange("B3");

var base_url = "https://www.instagram.com/" + user_id + "/?__a=1";
var pID = parseInt(urlfetch(base_url)['/logging_page_id']);
  
//Logger.log(pID);
  
    IDcell.setValue(pID);
};

function urlfetch(url) {
    var ignoreError = {
        "muteHttpExcecptions": true
    };
    //var url = "https://www.instagram.com/geomantiger/?__a=1";
    var source = UrlFetchApp.fetch(url, ignoreError).getContentText();
    var data = JSON.parse(source);
    return data;
  
  Logger.log(source);
}

P.S. I tried almost all suggested "import" functions:

importdata
importxml
importjson (custom library)

I researched a lot but didn't find a solution.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • "it imports JSON data as HTML" - You are wrong in this assumption. Actually look at the HTML you are receiving. It is an error page from Instagram because they can detect your request isn't coming from a browser. See [this related question on instagram JSON API restrictions](https://stackoverflow.com/questions/49788905/what-is-the-new-instagram-json-endpoint). – dwmorrin Aug 22 '20 at 11:36
  • Thanks for your response. I asked this question cause this approach worked for me for a few days. Now it's clear, it doesn't work. Found similar issues here: https://stackoverflow.com/questions/62496539/problem-with-getting-instagram-hashtag-json-without-api-into-google-sheets – HumanoVirtual Aug 22 '20 at 11:56

0 Answers0