Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Oct 28, 2016 Oct 28, 2016

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!

TOPICS
Actions and scripting
3.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Oct 28, 2016 Oct 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}).prop

...
Translate
Adobe
Advocate ,
Oct 28, 2016 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2016 Oct 28, 2016
LATEST

Yes the polyfill works, thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines