retrieve JSON data based on user selection/prompt

I am trying to have the user make a choice and based on that choice I will drill into the JSON data and display selected information. Ultimately I would like to create a dropdown selection in html and event listener in Javascript that would then go retrieve.

var userOcean = prompt("Will you be fishing in the gulf or atlantic ?");

var userFish = prompt("What fish do you want to look up?");

console.log(
    "\n\nfish:  "+jsonObject.ocean_measure.userOcean.fish.userFish.name+
    "\n\nlength:  "+jsonObject.ocean_measure.userOcean.fish.userFish.length+
    "\n\nclosed:  "+jsonObject.ocean_measure.userOcean.fish.userFish.closed+
    "\n\nlimit:  "+jsonObject.ocean_measure.userOcean.fish.userFish.limit+
    "\n\nremarks:  "+jsonObject.ocean_measure.userOcean.fish.userFish.remarks
    );

above is the Javascript and below is the JSON data

var jsonObject = {
"ocean_measure" : 
    {
    "gulf": 
        {
            "fish": {
                "dolphin": {
                    "name": "Mahi-mahi",
                    "length": "none",
                    "limit": "10 per person or 60 per vessel whichever is less"
                },
                "blackfin tuna": {
                    "name": "Blackfin Tuna",
                    "length": "not regulated",
                    "limit": "The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"
                },
                "snook": {
                    "name": "Snook",
                    "length": "Not less than 28 inches total length (TL) or more than 33 inches TL",
                    "closed": "Dec. 1-end of February; May 1-Aug. 31",
                    "limit": "1 per harvester per day",
                    "remarks": "Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."
                }
            }
        }
    ,
    "atlantic": 
        {
            "fish": {
                "dolphin": {
                    "name": "Mahi-mahi",
                    "length": "20 inches fork length",
                    "limit": "10 per person or 60 per vessel whichever is less"
                },
                "blackfin tuna": {
                    "name": "Blackfin Tuna",
                    "length": "not Regulated",
                    "limit": "The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"
                },
                "snook": {
                    "name": "Snook",
                    "length": "Not less than 28 inches total length (TL) or more than 32 inches TL",
                    "closed": "Dec. 15 to Jan. 31, June 1 to Aug. 31",
                    "limit": "1 per harvester per day",
                    "remarks": "Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."
                }
            }
        }
    }
}

I have been unable to find a simple way to take userInput and create a data retrieval with it from JSON file.

Answers:

Answer

You got it almost right, but if you want to use a variable while accessing an object you have to do it this way:

jsonObject.ocean_measure[userOcean].fish[userFish].name

Fixed console.log function:

console.log(
  "\n\nfish:  "+jsonObject.ocean_measure[userOcean].fish[userFish].name+
  "\n\nlength:  "+jsonObject.ocean_measure[userOcean].fish[userFish].length+
  "\n\nclosed:  "+jsonObject.ocean_measure[userOcean].fish[userFish].closed+
  "\n\nlimit:  "+jsonObject.ocean_measure[userOcean].fish[userFish].limit+
  "\n\nremarks:  "+jsonObject.ocean_measure[userOcean].fish[userFish].remarks
);

Also, JSFiddle

Answer

In order to use the content of a variable to access an object's properties, you must use what is called bracket notation:

ocean_measure.gulf == ocean_measure["gulf"]

This comes from the fact that in Javascript, objects act like dictionaries in that their properties and methods can be accessed with keys, the key being the name of the property or method.

In order for the user's choice, as a variable, to be selected from the object, put the variable in brackets:

userOcean = "gulf" 
ocean_measure[userOcean] == ocean_measure["gulf"]

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.