Reputation: 27
I have a file, sample.JSON with the following contents:
{ "resultset": {
"-count": "4904",
"-resultType": "campgrounds",
"result": [
{
"-availabilityStatus": "N",
"-contractID": "GA",
"-contractType": "STATE",
"-facilityID": "530145",
"-facilityName": "A. H. STEPHENS STATE HISTORIC PARK",
"-faciltyPhoto": "/webphotos/GA/pid530145/0/80x53.jpg",
"-latitude": "33.5633333",
"-longitude": "-82.8966667",
"-shortName": "C145",
"-sitesWithAmps": "Y",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "Y",
"-state": "GA"
},
{
"-availabilityStatus": "N",
"-contractID": "OH",
"-contractType": "STATE",
"-facilityID": "960023",
"-facilityName": "A.W. MARION STATE PARK",
"-faciltyPhoto": "/webphotos/OH/pid960023/0/80x53.jpg",
"-latitude": "39.6336111",
"-longitude": "-82.8747222",
"-shortName": "P023",
"-sitesWithAmps": "Y",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "N",
"-state": "OH"
},
{
"-availabilityStatus": "N",
"-contractID": "NRSO",
"-contractType": "FEDERAL",
"-facilityID": "72346",
"-facilityName": "ACKER ROCK LOOKOUT",
"-faciltyPhoto": "/webphotos/NRSO/pid72346/0/80x53.jpg",
"-latitude": "43.0523056",
"-longitude": "-122.6456111",
"-shortName": "ARCL",
"-sitesWithAmps": "N",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "N",
"-state": "OR"
}
] } }
This is just a sample of a much larger JSON object that I would like to go through and select out the facilityName from each array object (not sure if that's the right term), and then write each facilityName to a new file.
I know I need to load the JSON object, and then do a for loop for the length of the object. Right now I'm just trying to get the sample.JSON file to load. Here is my code so far:
var data = require('./campSample.JSON');
var fs = require('fs');
console.log(JSON.parse(data));
Trying to run this with node keeps giving me SyntaxError: Unexpected token :, referring to the colon in "resultset": {
I don't know why this would be. I used a JSON validator to confirm that the JSON is good. Why is it telling me that this syntax is wrong?
Upvotes: 0
Views: 226
Reputation: 887449
Change .JSON
in the filename to be lowercase.
require()
has special code to parse .json
files as JSON rather than Javascript, but this is case-sensitive.
Upvotes: 1
Reputation: 887449
require()
already parses your JSON.
You don't need to do anything.
Upvotes: 1