Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
JSON contains one object:
results[0] = { 'MAX(id)': 1 }
And this code doesn't work:
var text = results[0]; var obj = JSON.parse(text); console.log(obj.MAX(id));
results[0]
obj['Max(id)']
results[0] is already an object type
You can parse only from string to object like this:
JSON.parse('{ "MAX(id)": 1 }');
Add a comment
Your object is already a JSON. You don't need to parse it. To access MAX(id) property, you can use [] notation as follows:
MAX(id)
results[0] = { 'MAX(id)': 1 }; console.log(results[0]['MAX(id)']);
Your result[0] is a real javascript object. JSON.parse transforms text into objects, so you can't parse other objects with it.
var results = { 'MAX(id)': 1 }; //var text = results; //var obj = JSON.parse(text); alert(results['MAX(id)']);
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
results[0]is already an object, not a JSON string, so no need to parse it. Then you should useobj['Max(id)']because it's a string, not a method.