0

I am working with a Harp project, using Jade templates. I have my _data.json populated with content and I'm having trouble accessing it the way I thought I could. Given this JSON structure:

{
"mountain-residence": {
    "slug": "mountain-residence",
    "title": "Mountain Residence",
    "lead": "A southeast facing home...",
    "thumb": "exterior/entry",
    "sections": [
      {
        "exterior": {
          "slug": "exterior",
          "title": "Exterior Photos",
          "lead": "Embracing an entry...",
          "thumb": "terrace",
          "photos": [
            {
              "slug": "southeast",
              "alt": "Mountain Room Overlook",
              "caption": "Porch, deck and spa terrace"
            },
            {
              "slug": "terrace",
              "alt": "Southeast Terrace",
              "caption": "Spa deck and landscape terrace"
            },
            {
              "slug": "entry",
              "alt": "Entry Courtyard",
              "caption": "Entry court and pergola"
            },
            {
              "slug": "porch",
              "alt": "Entry Porch",
              "caption": "Timber entry shelter"
            }
          ]
        }
      },
      {
        "interior": {
          "slug": "interior",
          "title": "Interior Photos",
          "lead": "The interior spaces...",
          "thumb": "mountain-room2",
          "photos": [
            {
              "slug": "mountain-room2",
              "caption": "Entry opening to the Mountain Room"
            },
            {
              "slug": "dining",
              "caption": "Dining Room into Mountain Room"
            },
            {
              "slug": "cellar1",
              "caption": "Wine Cellar and Tasting Room"
            },
            {
              "slug": "den",
              "caption": "Den and Family Hearth"
            }
          ]
        }
      },
      {
        "design-sketches": {
          "slug": "design-sketches",
          "title": "Design Sketches",
          "lead": "A careful site...",
          "thumb": "shower",
          "photos": [
            {
              "slug": "schematic",
              "caption": "Exterior Elevation Study"
            },
            {
              "slug": "elevation",
              "caption": "Elevation Color Studies"
            },
            {
              "slug": "shower",
              "caption": "Outdoor stone shower"
            }
          ]
        }
      }
    ]
  }
}

I cannot figure out how to access a named object within an array, like mountain-residence.sections.exterior.title ("Exterior Photos"). Trying mountain-residence.sections['exterior'].title doesn't work either. I can access the same property with mountain-residence.sections[0].title, and my JSON seems to be valid, but I need to call the object with its name... Is there a way to do this?

For example, "mountain-residence" populates a page, and my general idea was to have each object in "sections" populate its own sub-page, which is why I want to be able to call it up by name through my JSON.

minttoothpick
  • 75
  • 2
  • 11
  • 2
    Can't really help you if we don't know which language you are using. Either way, I doubt `mountain-residence.sections.exterior.title` or `mountain-residence.sections[0].title` actually work. `sections` is an array of objects. All you can do is iterate over the array and compare each entry against the value you are looking for. If you are working with JavaScript, see [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) – Felix Kling Aug 10 '14 at 23:32
  • did you try mountain-residence.sections['exterior'].title – RST Aug 10 '14 at 23:36
  • Yeah, you will need to somehow iterate through the *sections* array, but how you do that depends on the language you have. Without that information this question is meaningless and might as well be closed. – Hot Licks Aug 10 '14 at 23:39

1 Answers1

0

As stated in the comments, sections is an array, so only the index notation will work. If you want to get it by the name of the section, it needs to be an object either natively, or else transformed into an object by preprocessing code you write.

Paul
  • 35,689
  • 11
  • 93
  • 122