JSON as a JavaScript Object

Consider a converter stack in a step that contains a Convert Using JavaScript converter. This converter gets access to the output from the previous converter as a variable named INPUT to use in the JavaScript used by the converter. The value of the INPUT variable is always a String.

The following table shows possible conversion values for the INPUT variable.

INPUT Value

JavaScript (OUTPUT =)

Result (OUTPUT value)

5

OUTPUT = INPUT

5

5

OUTPUT = INPUT + 3

53

5

OUTPUT = eval(INPUT)

5

5

OUTPUT = eval(INPUT) + 3

8

5

OUTPUT = eval(INPUT + 3)

53

5

OUTPUT = eval(INPUT + " + 3")

8

[1,2,3]

OUTPUT = INPUT[0]

[

[1,2,3]

OUTPUT = eval(INPUT)[0]

1

{ “a”: 5 }

OUTPUT = eval(INPUT).a

“Syntax Error”

{ “a”: 5 }

OUTPUT = eval("var x=" + INPUT + "; x;").a;

5

Note the following when converting JSON to JavaScript:

  • INPUT is a variable bound to a string value. Therefore, any operation that you perform on INPUT is a string operation. For example, + is string concatenation. That is why INPUT + 3 becomes 53 in the example above.
  • The function "eval" only accepts correct JavaScript as input and {"a":5} is not a syntactically correct JavaScript line, but var x = {"a":5} is, which is why the last example above is the one that works.