user3570620 user3570620 - 6 months ago 10
HTML Question

What causes the 'Uncaught SyntaxError: missing )' in this code example?

Why am I getting this error message when running the code below.


Uncaught SyntaxError: missing ) after argument list


<html>
<head>
<title>read json</title>
<script type="text/javascript">
var load = function() {
var data='{"took": 2,"timed_out": false,"_pieces": { "total": 5, "successful": 5, "failed": 0},"hits": { "total": 117, "max_score": 0, "hits": []},"aggregations": { "2": { "doc_error": 228, "sum_other_t": 10705, "buckets": [ { "key": "2016", "count": 196 }, { "key": "2015", "count": 163 }, { "key": "2014", "count": 159 }, { "key": "2013", "count": 157 }, { "key": "2012", "count": 157 } ] }}}';
var myData = JSON.parse(data);
console.log(myData.aggregations.2.buckets.length) ;
//console.log(myData) ;
}
</script>
</head>
<body>
hi
</body>
</html>

Answer

As per the docs dot notation can only use when

In this code, property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object.$1 is valid, while object.1 is not.


So you need to use bracket notation([]) to access property 2

<html>

<head>
  <title>read json</title>
  <script type="text/javascript">
    var load = function() {
      var data = '{"took": 2,"timed_out": false,"_pieces": {  "total": 5,  "successful": 5,  "failed": 0},"hits": {  "total": 11537,  "max_score": 0,  "hits": []},"aggregations": {  "2": {    "_upper_bound": 228,    "doc_count": 10705,    "buckets": [      {        "key": "2016",        "count": 196      },      {        "key": "2015",        "count": 163      },      {        "key": "2014",        "count": 159      },      {        "key": "2013",        "count": 157      },      {        "key": "2012",        "count": 157      }    ]  }}}';
      var myData = JSON.parse(data);
      console.log(myData.aggregations[2].buckets.length);
      //console.log(myData) ;   
    }
  </script>
</head>

<body onload="load()">
  hi
</body>

</html>


FYI : Also there is no need to define it as a JSON string, it's also a valid object format in JavaScript you can define it as object.

<html>

<head>
  <title>read json</title>
  <script type="text/javascript">
    var load = function() {
      var data = {"took": 2,"timed_out": false,"_pieces": {  "total": 5,  "successful": 5,  "failed": 0},"hits": {  "total": 11537,  "max_score": 0,  "hits": []},"aggregations": {  "2": {    "doc_count_error_upper_bound": 228,    "sum_other_doc_count": 10705,    "buckets": [      {        "key": "2016",        "count": 196      },      {        "key": "2015",        "count": 163      },      {        "key": "2014",        "count": 159      },      {        "key": "2013",        "count": 157      },      {        "key": "2012",        "count": 157      }    ]  }}};
      console.log(data.aggregations[2].buckets.length);
      //console.log(myData) ;   
    }
  </script>
</head>

<body onload="load()">
  hi
</body>

</html>