Skip to content

Instantly share code, notes, and snippets.

@faceyspacey
Last active January 30, 2018 23:18
Show Gist options
  • Save faceyspacey/66eced9544bcc386c1bcf128038f9302 to your computer and use it in GitHub Desktop.
Save faceyspacey/66eced9544bcc386c1bcf128038f9302 to your computer and use it in GitHub Desktop.
const json = `{
"Status": "Success",
"Errors": [],
"Paging": [],
"Notices": [],
"Message": null,
"Data": {
"certifications": [
{
"name": "American Standard Certification",
"required": true,
"company_id": 87,
"recommended": false,
"link": "AmericanStandardProducts",
"id": 40,
"blurb": "American Standard and LIXIL background information. This quiz emphasizes Lixil and American Standard history as presented in the film, and the installation instructions Craftsmen need to install offered products."
}, {
"name": "Ferguson",
"required": true,
"company_id": 79,
"recommended": true,
"link": "FergusonCertification",
"id": 35,
"blurb": "Appliance repair needed for both in and out of warranty, a very short review of Ferguson (less than two minutes)"
}, {
"name": "Craftsman Republic",
"required": true,
"company_id": 0,
"recommended": false,
"link": "CraftsmanRepublicCertifiedCraftsman",
"id": 31,
"blurb": "This certification will inform you of who Craftsman Republic is, how to work with us and what you can expect. Some of our Clients ask for additional certifications specific to them but getting Craftsman Republic Certified is your first step to receiving Workorders from us."
}
]
}
}`
// typically the above data would be received as a string as part of an AJAX request
// or by reading a file from disk, etc.
const result = JSON.parse(json) // once you get the JSON as a string, you call JSON.parse on it to turn it into javascript
// NOTE: JSON by the way is just standard javascript object notation (made of arrays + primitives like strings and numbers),
// but in string form, with a few other restrictions.
// Lastly, since you mentioned "filtering," here's an example of taking an array in the object structure,
// and filtering it to only return certifications with truthy values for `.recommended`:
const certs = result.Data.certifications.filter(cert => cert.recommended)
// Also note: what we did here was first key into nested objects until we reached `.certifications`.
// IF YOU FOUND THIS HELPFUL, FEEL FREE TO TIP VIA A ONE-TIME PAYMENT ON CODE MENTOR :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment