Home > Mobile >  How do I fix: SyntaxError: Unexpected token u in JSON at position 1
How do I fix: SyntaxError: Unexpected token u in JSON at position 1

Time:11-01

function json_data () {

  const url = "https://in.investing.com/economic-calendar/historical-data-table?eventId=201"
  
  const res = UrlFetchApp.fetch(url,{muteHttpExceptions: true});

  const data = JSON.parse(JSON.parse(res.getContentText));

  const result = data.map(post => {
    return [post["release_date"],post["actual"]]
  })
  return result
}

this is my first time writing in a programming language, I am running this program code in Google-Sheet App-Script. the result I am receiving either comes as null or undefined, I am not even sure if it is the proper JSON file I am requesting. a segment of source URL I am trying to request:

{"release_date":"Oct 24, 2022 (Oct)","time":"13:30","actual":"46.6","forecast":"47.8","previous":"48.4","metaData":{"timestamp":1666598400,"perliminary":true,"color":{"actual":"u-down","previous":""}

CodePudding user response:

change this line :

const data = JSON.parse(JSON.parse(res.getContentText));

to

const data = JSON.parse(JSON.stringify(res.getContentText()));

( also you can remove extra JSON.parse(JSON.stringify()) entirely )

  • Related