JSON.parse - Passing variable into query.outfields.

4034
4
01-27-2015 11:48 AM
KeithAnderson
New Contributor III

Friends:

I am trying to pass a variable into query.outfields.

When it is hard coded it works. Coming in as an object.

query.outFields = ["OBJECTID", "NAME"];

Passing in the JSON variable does not work.

query.outFields = JSON.parse(FieldNames);    //   "['OBJECTID', 'Name']" (value in query parameter through FFox)

I am getting a JSON error in the browser that says it cannot recognize the square bracket [ .

Any suggestions on how to pass in JSON variables into query.output would be great.

Keith

Tags (1)
0 Kudos
4 Replies
JeffJacobson
Occasional Contributor III

I think the problem is the single quotes around OBJECTID and Name. JSON only allows double-quotes as string delimiters. You'll need to replace the single quote characters with double quotes.

0 Kudos
KeithAnderson
New Contributor III

I have tried everything I know without any luck.

"extentlatlon":"[-93.45,44.93]", JSON.parse on that extent works.

"FeatureOutputFields":"[OBJECTID,NAME]" JSON.parse on those fields do not.

"FeatureOutputFields":"[“OBJECTID,NAME”]” No go

"FeatureOutputFields":"[“OBJECTID”,”NAME”]” No go

Thanks for the effort.

0 Kudos
NicholasHaney
New Contributor III

The following method worked for me:

var temp = '{"FeatureOutputFields":["SQMI", "STATE_NAME", "STATE_FIPS", "SUB_REGION", "STATE_ABBR"]}';

var temp1 = JSON.parse(temp);  

query.outFields = temp1.FeatureOutputFields;

You can test the application here: Edit fiddle - JSFiddle

KeithAnderson
New Contributor III

Thanks Nic.

I will remember your effort!

Jeff told me to try escape characters and I eventually got it going.

You'll need to escape the double quotes since the string is delimited by double quotes.

{
    "FeatureOutputFields": "[\"OBJECTID,NAME\"]"
}

This was the final result for me:

"FeatureOutputFields":"[\"OBJECTID\",\"NAME\"]"

Keith

0 Kudos