0

So I have a function that makes an ajax call and returns a json string. I am having trouble trying to access the values that I need, below is my code of what I have and a few examples of what I have tried.

    s.search().then(function (specials) {
        var returnJSON = JSON.parse(specials[0]);
        var x = returnJSON.location.x;
        var y = returnJSON.location.y;
        });

When I check the dev tools I'm getting the following error.

 JSON.parse: unexpected character at line 1 column 2 of the JSON data

Here is the the JSON returned value after I stringify it.

[{"feature":{"geometry":{"type":"point","x":-82.9172080701955,"y":42.55426092899978,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Postal","Match_addr":"48035, Clinton Township, Michigan","StAddr":"","City":"Clinton Township","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-82.922209,"ymin":42.549261,"xmax":-82.912209,"ymax":42.559261,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"48035, Clinton Township, Michigan"},{"feature":{"geometry":{"type":"point","x":-84.03589825899667,"y":44.826904141314174,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton Twp, Michigan","StAddr":"","City":"Clinton Twp","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-84.085899,"ymin":44.776904,"xmax":-83.985899,"ymax":44.876904,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton Twp, Michigan"},{"feature":{"geometry":{"type":"point","x":-83.93987906956261,"y":42.065412162742234,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton Twp, Michigan","StAddr":"","City":"Clinton Twp","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-83.98988,"ymin":42.015412,"xmax":-83.88988,"ymax":42.115412,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton Twp, Michigan"},{"feature":{"geometry":{"type":"point","x":-82.93354923650725,"y":42.60054198222781,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton Twp, Michigan","StAddr":"","City":"Clinton Twp","score":100},"infoTemplate":null},"extent":{"type":"extent","xmin":-82.98355,"ymin":42.550542,"xmax":-82.88355,"ymax":42.650542,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton Twp, Michigan"},{"feature":{"geometry":{"type":"point","x":-83.97095926895429,"y":42.07240087260328,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"Locality","Match_addr":"Clinton, Michigan","StAddr":"","City":"Clinton","score":94.29},"infoTemplate":null},"extent":{"type":"extent","xmin":-84.02096,"ymin":42.022401,"xmax":-83.92096,"ymax":42.122401,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton, Michigan"},{"feature":{"geometry":{"type":"point","x":-84.6015125489642,"y":42.943655651388326,"spatialReference":{"wkid":102100,"latestWkid":3857}},"symbol":null,"attributes":{"Addr_type":"SubAdmin","Match_addr":"Clinton, Michigan","StAddr":"","City":"Clinton","score":94.29},"infoTemplate":null},"extent":{"type":"extent","xmin":-84.839514,"ymin":42.705656,"xmax":-84.363514,"ymax":43.181656,"spatialReference":{"wkid":102100,"latestWkid":3857}},"name":"Clinton, Michigan"}]

I am trying to access candidates location x value and y value.

6
  • JSLint says that is invalid! (jslint.com) Commented Aug 12, 2015 at 20:21
  • it seems like this is weird: "y":42 .072400872603282 Commented Aug 12, 2015 at 20:22
  • The json is returned from a mapping service. Even if i change it from JSON.parse to JSON.stringify I am still having issues getting the data i need Commented Aug 12, 2015 at 20:27
  • Youve just updated your question, and invalidated the answers given! The JSON you now have in the question is valid. Commented Aug 12, 2015 at 20:28
  • JSON.parse and JSON.stringify do very different things, like opposite different! Why would you use them interchangeably? Commented Aug 12, 2015 at 20:29

2 Answers 2

6

There are some Strings in your JSON on separate lines. When you copy and paste your JSON in a linter (e.g.: json linter), you will see the errors.

EDIT: You edited your question so you are now using valid JSON.

There is no need to parse your JSON when you already have valid JSON. You can just select the correct keys. Looking at your JSON, you can select your x and y like this:

var returnJSON = specials[0];
var x = returnJSON.feature.geometry.x;
var y = returnJSON.feature.geometry.y;

Checkout this codepen for an example.

Sign up to request clarification or add additional context in comments.

1 Comment

Its not helping the readability of this question/answer with two huge blocks of JSON in the middle :( You can delete this answer now that its no longer relevant (or change it of course
0

You don't need to "parse" JSON returned from a service usually. It is already a Javascript object in the then callback.

s.search().then(function (specials) {
    var firstItem = specials[0];
    var x = firstItem.location.x;
    var y = firstItem.location.y;
});

It should be noted, however, that the first item that comes back in your array of specials does not have a property location accoring to the JSON example you posted.

Perhaps you were after firstItem.feature.geometry.x, but im not sure.

3 Comments

This actually does not work. firstItem.location is undefined
updated my code and it's still not working. TypeError: firstItem.feature is undefined
@pullmyhair then your json isn't what you've shown us in your question, because if I paste your json into jsfiddle and try to read that property then it works just fine. (jsfiddle.net/e0c3g1hm) you have removed all JSON.parse/JSON.stringify from your code, right?

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.