Skip to main content
Participating Frequently
October 28, 2016
Answered

Parsing JSON in JSX: ReferenceError: Object.keys is not a function

  • October 28, 2016
  • 1 reply
  • 3402 views

I'm getting a "ReferenceError: Object.keys is not a function" error when trying to parse JSON from a jsx file:

Inside my jsx file I have:

$.evalFile("../json2.js");

try

{

     var testDict = {"a": "1", "b": "2"};

     var keys = Object.keys(testDict);

catch (e) {

     alert("Caught error: " + e);

}

Does anyone have any idea why I'm getting this error when trying to parse the keys?

Thanks!

This topic has been closed for replies.
Correct answer DBarranca

Sure it can't work... Object.keys is an EcmaScript 5 feature, and ExtendScript is stuck at version 3 (that's the why you're importing json2.js – no native json support in this dry land). According to Mozilla, this polyfill will work:

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

if (!Object.keys) {

  Object.keys = (function () {

    'use strict';

    var hasOwnProperty = Object.prototype.hasOwnProperty,

        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),

        dontEnums = [

          'toString',

          'toLocaleString',

          'valueOf',

          'hasOwnProperty',

          'isPrototypeOf',

          'propertyIsEnumerable',

          'constructor'

        ],

        dontEnumsLength = dontEnums.length;

    return function (obj) {

      if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {

        throw new TypeError('Object.keys called on non-object');

      }

      var result = [], prop, i;

      for (prop in obj) {

        if (hasOwnProperty.call(obj, prop)) {

          result.push(prop);

        }

      }

      if (hasDontEnumBug) {

        for (i = 0; i < dontEnumsLength; i++) {

          if (hasOwnProperty.call(obj, dontEnums)) {

            result.push(dontEnums);

          }

        }

      }

      return result;

    };

  }());

}

Hope this helps,

Davide Barranca

www.davidebarranca.com

http://htmlpanelsbook.com

1 reply

DBarranca
DBarrancaCorrect answer
Legend
October 28, 2016

Sure it can't work... Object.keys is an EcmaScript 5 feature, and ExtendScript is stuck at version 3 (that's the why you're importing json2.js – no native json support in this dry land). According to Mozilla, this polyfill will work:

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

if (!Object.keys) {

  Object.keys = (function () {

    'use strict';

    var hasOwnProperty = Object.prototype.hasOwnProperty,

        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),

        dontEnums = [

          'toString',

          'toLocaleString',

          'valueOf',

          'hasOwnProperty',

          'isPrototypeOf',

          'propertyIsEnumerable',

          'constructor'

        ],

        dontEnumsLength = dontEnums.length;

    return function (obj) {

      if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {

        throw new TypeError('Object.keys called on non-object');

      }

      var result = [], prop, i;

      for (prop in obj) {

        if (hasOwnProperty.call(obj, prop)) {

          result.push(prop);

        }

      }

      if (hasDontEnumBug) {

        for (i = 0; i < dontEnumsLength; i++) {

          if (hasOwnProperty.call(obj, dontEnums)) {

            result.push(dontEnums);

          }

        }

      }

      return result;

    };

  }());

}

Hope this helps,

Davide Barranca

www.davidebarranca.com

http://htmlpanelsbook.com

Participating Frequently
October 28, 2016

Yes the polyfill works, thanks!