pjlamb12
pjlamb12

Reputation: 2422

Can't do JSON.parse on data from SQL Server

I have a query that gets data from SQL Server, and in one of the fields we are storing JSON as a varchar(max). The issue is that when I try to get the data out in my Node app and do a JSON.parse, it's not working. Here's what I've tried:

console.log(data.attr);
    // "{ firstName: 'Preston', lastName: 'Lamb' }"
JSON.parse(data.attr) 
    // Invalid expression: unexpected end of input (get this on one item)
    // invalid expression: unexpected token f in json at position 2 (and this on the other)
newJsonStr = JSON.stringify(data.attr)
    // ""{ firstName: 'Preston', lastName: 'Lamb' }""
newJson = JSON.parse(newJsonStr)
    // "{ firstName: 'Preston', lastName: 'Lamb' }"

enter image description here

None of this is really complicated stuff...it should be easy to JSON.parse and/or JSON.stringify, but it doesn't work. Any ideas at all?

Upvotes: 0

Views: 704

Answers (1)

chairmanmow
chairmanmow

Reputation: 650

What the other commentator meant by it's not JSON is that your keys need to be wrapped in quotes to be parsed as JSON.

JSON Spec - does the key have to be surrounded with quotes?

Upvotes: 1

Related Questions