0

I am getting an error trying to parse a JSON structure with Node.js, Express and DoT. It appears to be processing special characters in the JSON object below...

<td>
  {{= "Overview: " + record._source.explanation.overview + " Name: " + record._source.explanation.detailed-explanation.name }}
</td>

The first part of the query works fine (no "-" character in the structure), but when I try to access record._source.explanation.detailed-explanation.name it errors out with the message below.

undefined:2
source.explanation.analysis + " " + record._source.explanation.detailed-explanation.name
                                                                    ^
ReferenceError: explanation is not defined
    at Object.eval (eval at <anonymous> (/myapp/node_modules/express-dot/node_modules/dot/doT.js:125:11), <anonymous>:2:1715)
    at /myapp/node_modules/express-dot/express-dot.js:23:30
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

I have tried to escape the "-" character, such as '-' etc, but with no luck. Not really an option to go back and restructure the underlying JSON to remove '-'s. Any ideas on how to get past the special character?

Thanks!!!

JstnPwll
  • 8,585
  • 2
  • 33
  • 56
Alex
  • 397
  • 4
  • 18
  • possible duplicate of [How do I reference a javascript object property with a hyphen in it](http://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) and [Unable to access JSON property with “-” dash](http://stackoverflow.com/questions/13869627/unable-to-access-json-property-with-dash) – Protomen Nov 16 '14 at 01:18

1 Answers1

2

You need to use square bracket notation instead of dot notation when accessing properties with names that are not valid javascript identifiers.

<td>
  {{= "Overview: " + record._source.explanation.overview + " Name: " + record._source.explanation["detailed-explanation"].name }}
</td>
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274