-1

I am using an api and by that I am getting this response

{
"categories": [
[
  "Related Queries",
  [
    {
      "value": "tower",
      "formInput": "tower"
    },
    {
      "value": "erection",
      "formInput": "erection"
    },
    {
      "value": "police protection",
      "formInput": "police%20protection"
    },
    {
      "value": "to provide police protection",
      "formInput": "to%20provide%20police%20protection"
    },
    {
      "value": "phone tower",
      "formInput": "phone%20tower"
    }
  ]
],
[
  "Document Types",
  [
    {
      "value": "All",
      "formInput": "jio",
      "selected": true
    },
    {
      "value": "Laws",
      "formInput": "jio+doctypes:laws"
    },
    {
      "value": "Judgments",
      "formInput": "jio+doctypes:judgments"
    },
    {
      "value": "Highcourts",
      "formInput": "jio+doctypes:highcourts"
    },
    {
      "value": "HC & SC",
      "formInput": "jio+doctypes:supremecourt,scorders,highcourts"
    }
  ]
],
[
  "Courts",
  [
    {
      "value": "chennai",
      "formInput": "jio+doctypes:chennai",
      "selected": false
    },
    {
      "value": "kerala",
      "formInput": "jio+doctypes:kerala",
      "selected": false
    },
    {
      "value": "cat_delhi",
      "formInput": "jio+doctypes:cat_delhi",
      "selected": false
    },
    {
      "value": "bombay",
      "formInput": "jio+doctypes:bombay",
      "selected": false
    },
    {
      "value": "delhi",
      "formInput": "jio+doctypes:delhi",
      "selected": false
    },
    {
      "value": "supremecourt",
      "formInput": "jio+doctypes:supremecourt",
      "selected": false
    },
    {
      "value": "patna",
      "formInput": "jio+doctypes:patna",
      "selected": false
    },
    {
      "value": "patna_orders",
      "formInput": "jio+doctypes:patna_orders",
      "selected": false
    },
    {
      "value": "jodhpur",
      "formInput": "jio+doctypes:jodhpur",
      "selected": false
    },
    {
      "value": "madhyapradesh",
      "formInput": "jio+doctypes:madhyapradesh",
      "selected": false
    }
  ]
],
[
  "Authors",
  [
    {
      "value": "K Venkataswami",
      "formInput": "jio+author:K Venkataswami"
    },
    {
      "value": "S Sanyal",
      "formInput": "jio+author:S Sanyal"
    },
    {
      "value": "S.C.Sharma",
      "formInput": "jio+author:S.C.Sharma"
    }
  ]
],
[
  "Years",
  [
    {
      "value": 2015,
      "formInput": "jio+fromdate:1-1-2015+todate:31-12-2015"
    },
    {
      "value": 2016,
      "formInput": "jio+fromdate:1-1-2016+todate:31-12-2016"
    },
    {
      "value": 2014,
      "formInput": "jio+fromdate:1-1-2014+todate:31-12-2014"
    },
    {
      "value": 2019,
      "formInput": "jio+fromdate:1-1-2019+todate:31-12-2019"
    },
    {
      "value": 2011,
      "formInput": "jio+fromdate:1-1-2011+todate:31-12-2011"
    }
  ]
]
],
  "docs": [
{
  "title": "M/S. Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 22 January, 2016",
  "covertitles": [
    
  ],
  "numcites": 2,
  "numcitedby": 0,
  "url": "http://judis.nic.in/Judis_Chennai/list_new2.asp?FileName=259280",
  "headline": "Madras High Court \n M/S. Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 22 January, 2016          \n\n \nIN THE HIGH ... Reliance <b>Jio</b> Infocomm Limited\nhaving its circle office at \nA1 Tower, 8th Floor,\n89-90, Dr.Radhakrishnan Salai,\nMylapore, \nChennai",
  "docsize": 3785,
  "tid": 90453177,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2016-01-22",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 29 January, 2016",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/Judis_Chennai/list_new2.asp?FileName=260428",
  "headline": "Madras High Court \n M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 29 January, 2016          \n\n \nIN THE HIGH ... Reliance <b>Jio</b> Infocomm Limited,\nHaving its Circle Office at\nA1 Tower, 8th Floor,\n89-90, Dr.Radhakrishnan Salai,\nMalpore Chennai",
  "docsize": 3458,
  "tid": 52215865,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2016-01-29",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 26 August, 2015",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/Judis_Chennai/list_new2.asp?FileName=239394",
  "headline": "Madras High Court \n M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 26 August, 2015          \n\n \nIN THE HIGH ... Reliance <b>Jio</b> Infocomm Limited\t\t\t\t\t\nHaving its Circle Office at\nA1 Tower, 8th Floor,\n89-90, Dr.Radhakrishnan Salai,\nMylapore, Chennai",
  "docsize": 3599,
  "tid": 121257329,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2015-08-26",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S Reliance <b>Jio</b> Infocomm Ltd. vs The State Of Bihar & Ors on 10 August, 2015",
  "covertitles": [
    
  ],
  "numcites": 2,
  "numcitedby": 0,
  "url": null,
  "headline": "Patna High Court - Orders \n M/S Reliance <b>Jio</b> Infocomm Ltd. vs The State Of Bihar &amp; Ors on 10 August ... Case No.7591 of 2015\n======================================================\n1. M/s Reliance <b>JIO</b> Infocomm Ltd., a companywithin the meaning of the\n Companies",
  "docsize": 7026,
  "tid": 22796076,
  "covertids": [
    
  ],
  "doctype": 45,
  "publishdate": "2015-08-10",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Patna High Court - Orders",
  "covers": [
    
  ]
},
{
  "title": "M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 26 August, 2015",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/Judis_Chennai/list_new2.asp?FileName=239260",
  "headline": "Madras High Court \n M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 26 August, 2015          \n\n \nIN THE HIGH ... Reliance <b>Jio</b> Infocomm Limited\t\t\t\t\t\nHaving its Circle Office at\nA1 Tower, 8th Floor,\n89-90, Dr.Radhakrishnan Salai,\nMylapore, Chennai",
  "docsize": 3547,
  "tid": 17100782,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2015-08-26",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S.Reliance <b>Jio</b> Inforcomm ... vs The Assistant Engineer on 26 August, 2015",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/Judis_Chennai/list_new2.asp?FileName=239488",
  "headline": "Madras High Court \n M/S.Reliance <b>Jio</b> Inforcomm ... vs The Assistant Engineer on 26 August, 2015          \n\n \nIN THE HIGH COURT ... Reliance <b>Jio</b> Inforcomm Limited,\nhaving Circle Office at A1 Tower,\n8th Floor, 89-90, Dr.Radhakrishnan Salai,\nMylapore, Chennai",
  "docsize": 4056,
  "tid": 76372287,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2015-08-26",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 15 April, 2015",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/judis_chennai/qrydisp.aspx?filename=77566",
  "headline": "Madras High Court \n M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 15 April, 2015         \n\n  \n\n   \n\n \n \n BEFORE THE MADURAI ... 6885 of 2015:\n\nM/s.Reliance <b>Jio</b> Infocomm Limited,\nhaving its Circle Office at A1 Tower, 8th Floor",
  "docsize": 5103,
  "tid": 187432957,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2015-04-15",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S.Reliance <b>Jio</b> Infocomm ... vs The Assistant Engineer on 19 March, 2015",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/judis_chennai/qrydisp.aspx?filename=77273",
  "headline": "Madras High Court \n M/S.Reliance <b>Jio</b> Infocomm ... vs The Assistant Engineer on 19 March, 2015         \n\n  \n\n   \n\n \n \n BEFORE THE MADURAI BENCH ... 16988 of 2014\n\n\nM/s.Reliance <b>Jio</b> Infocomm Limited,\nhaving Circle office at A1 Tower, 8th Floor",
  "docsize": 4761,
  "tid": 76302150,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2015-03-19",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S. Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 10 February, 2015",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/judis_chennai/qrydisp.aspx?filename=212570",
  "headline": "Madras High Court \n M/S. Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 10 February, 2015         \n\n  \n\n   \n\n \n \n IN THE HIGH ... Reliance  <b>Jio</b> Infocomm Limited,\nhaving its Circle office at A1 Tower, 8th Floor,\n89-90, Dr. Radhakrishnan Salai,\nMylapore, Chennai",
  "docsize": 3804,
  "tid": 73976485,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2015-02-10",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
},
{
  "title": "M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 18 February, 2015",
  "covertitles": [
    
  ],
  "numcites": 1,
  "numcitedby": 0,
  "url": "http://judis.nic.in/judis_chennai/qrydisp.aspx?filename=214508",
  "headline": "Madras High Court \n M/S.Reliance <b>Jio</b> Infocomm ... vs The Inspector Of Police on 18 February, 2015         \n\n  \n\n   \n\n \n \n IN THE HIGH ... Reliance <b>Jio</b> Infocomm Limited,\nhaving its Circle Office at A1 Tower, 8th Floor,\n89-90, Dr.Radhakrishnan Salai,\nMylapore, Chennai",
  "docsize": 3361,
  "tid": 171042175,
  "covertids": [
    
  ],
  "doctype": 24,
  "publishdate": "2015-02-18",
  "firstname": null,
  "secondname": null,
  "lastname": null,
  "fragment": true,
  "docsource": "Madras High Court",
  "covers": [
    
  ]
}
  ],
  "found": "11 - 20 of 1436",
  "encodedformInput": "jio"
}1

at the end of the response I am getting 1 in the response and this 1 is creating problem for me for parsing json. My javascript file is as below

function getData(){
var queryData = { 'query': 'jio'};
  $.ajax({
    type: "POST",
     url: 'api.php',
     data: queryData,
     dataType: 'html',
     success: function(response) {
       console.log(response);
}
            
      });

my PHP file which is getting API data is as below

<?PHP 
extract($_POST,EXTR_OVERWRITE);
    $headr = array();
    $headr[] = 'X-MESSAGE:' .base64_encode('dfsf');
    $headr[] = 'X-EMAIL: p******@gmail.com';
    $headr[] = 'Authorization:Token **************e453966e';

    $url = "https://api.indiankanoon.org/search/?formInput=$query&pagenum=1";
    $crl = curl_init();
    curl_setopt($crl, CURLOPT_URL, $url);
    curl_setopt($crl, CURLOPT_HTTPHEADER, $headr);
    curl_setopt($crl, CURLOPT_POST, true);
    $rest = curl_exec($crl);
    if ($rest === false)
    {
    // throw new Exception('Curl error: ' . curl_error($crl));
            print_r('Curl error: ' . curl_error($crl));
    }
    curl_close($crl);
    // $rest = json_encode($rest, JSON_PRETTY_PRINT);
    
    echo json_decode($rest);
    exit;
?>

The error I am getting is as follows

Uncaught SyntaxError: Unexpected number in JSON at position 8986

How to solve this error

1

1 Answer 1

1

Quick and dirty, I would do as follows within the PHP code

$rest = substr($rest, 0, -1);
4
  • I wouldnt call this "dirty". Its not a hack job. Its the solution. .... Provided a trim() is also applied just in case.
    – GetSet
    Jan 10, 2021 at 10:38
  • Thanks Marcho Bishchoff this solved my problem. Jan 10, 2021 at 10:41
  • @GetSet it depends, if the "1" comes directly from the JSON Response then its the solution, if it comes from the PHP code then call it quick and dirty. Jan 10, 2021 at 11:06
  • I'd say it doesnt come from the JSON because then it would be invalid JSON.
    – GetSet
    Jan 10, 2021 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.