0

Here is what I'm working with.

jsonobject = {
  "products": [
    {
      "ProductABC-001": {
        "attributes": [
          {
            "color": "blue"
          },
          {
            "size": "large"
          }
        ]
      }
    }
  ]
};
  1. Is this a true/pure JSON object or is this considered something else?
  2. If the answer is "no" what's different from it and a pure/true JSON object?
  3. Why does the below return "undefined" instead of "Array"? From my perspective I am in the first element of the products node which means the next level is an array of attributes. This, apparently, is wrong.

jsonobject.products[0].attributes[0]

giraffeslacks
  • 77
  • 1
  • 9
  • 2
    Did you mean `jsonobject.products[0]['ProductABC-001'].attributes[0]`? – elclanrs Dec 27 '14 at 22:41
  • 1
    1) No, it's a (JavaScript) assignment statement, assigning an object to a variable. 2) JavaScript is a programming language, JSON is a data format. 3) It would throw an error, not return `undefined`. I recommend to read [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196). – Felix Kling Dec 27 '14 at 22:46

2 Answers2

4
  1. There is no JSON here at all. What you have is a Javascript object literal that is used to create a Javascript object.

  2. JSON is a text representation of data. The JSON syntax is a subset of Javascript object and array literals syntax. The object literal that you have between the = and the ; happens to follow the more strict syntax for JSON, so you could take that part of the source code and use as JSON.

  3. Because the object that you get from jsonobject.products[0] doesn't have any attributes property.

You would use jsonobject.products[0]['ProductABC-001'].attributes[0], which returns the object { "color": "blue" }


From what I can see, all that you would need is an array of product objects, that has a name and an object with attributes:

var products = [
  {
    name: "ProductABC-001",
    attributes: {
      color: "blue",
      size: "large"
    }
  }
];
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks. Can you offer a more sane approach to the structure I have here? My gut says I've made this more complicated than it should be. – giraffeslacks Dec 27 '14 at 23:02
  • @giraffeslacks: For starters: `{"attributes": {"color": "blue", "size": "large"}}`. – Felix Kling Dec 27 '14 at 23:06
  • @giraffeslacks: I added a suggestion for a simpler format above. However, I don't know of your exact needs, so it might be *too* simple, but you be the judge of that. – Guffa Dec 27 '14 at 23:10
0

1/2. No it is not, the = sign is not part of the JSON spec. You would have to remove the jsonobject = bit for that to be "pure" JSON. (You can validate any JSON here: http://jsonlint.com)

For 3, @davin-tryon is correct, you're missing the ["ProductABC-001"] bit which is where "attributes" resides. Try this:

jsonobject.products[0]["ProductABC-001"].attributes[0]
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55