0

beginner with js here. I'm trying to parse a json string with node6. The interesting bit of json goes like this:

{
"Metadata" : {
    "AWS::CloudFormation::Interface" : {
        "ParameterGroups" : [
          {
            "Label" : {
                "default": "Group1"
            },
            "Parameters" : [
                "One",
                "Two"
            ]
          },
          {
            "Label" : {
                "default": "Group2"
            },
            "Parameters" : [
                "Three"
            ]
          }
       ]
    }
}
}

I'm trying to list all Parameters (One, Two, Three), but I cannot get through "AWS::CloudFormation::Interface". Accessing AWS::CloudFormation::Interface.ParameterGroups fails, and trying to walk AWS::CloudFormation::Interface subtree

for ( a in Metadata ) { 
    for ( b in a ) {}
} 

get's me an array of single characters.

thanks.

bartekmo
  • 208
  • 1
  • 7
  • 1
    Possible duplicate of [How to access object properties containing special characters?](https://stackoverflow.com/questions/12953704/how-to-access-object-properties-containing-special-characters) – Tomalak Sep 11 '17 at 11:10
  • *"I'm trying to parse a json string with node6."* Obviously you are already finished with parsing and have an *object*. Don't confuse JSON and objects. When it's a string that contains curly braces, it's JSON. When it has keys and values, it's an object. You can transform the one to the other, but they are not the same thing. – Tomalak Sep 11 '17 at 11:14
  • The act of accessing values inside an object by their key is not called "parsing". It's called "accessing values" (or "accessing properties"). The act of reading a string and producing the object that it describes is called "parsing" (the opposite operation is called "serialization"). – Tomalak Sep 11 '17 at 11:16
  • 1
    The value of `b` in your final example will be a string representing the key. I believe you intended to write `for (b in Metadata[a]) {}` or similar. That's a bit by the by though, as already noted the real solution here is to use square bracket notation to access `Metadata['property::name']`. – skirtle Sep 11 '17 at 14:12
  • @skirtle - thanks, square bracket notation does solve it. (BTW, double for loop was intended to get a pointer to the problematic object once I faced problems accessing it using object.property). – bartekmo Sep 12 '17 at 21:43
  • @Tomalak - actually, my goal *was* to parse json and get list of Parameters. Context usually helps. Obviously, technically you're right - I already did have the json parsed (not a big deal in node, is it)... do you know that old Microsoft helicopter joke? ;) – bartekmo Sep 12 '17 at 21:54
  • "Use the right words for things" is not useless tip. And I gave you the solution to your problem for free on top of that, so cut out the faux superiority. :) – Tomalak Sep 13 '17 at 03:29

0 Answers0