1

I'm using nodejs, and I'm trying to access a json key, which name containes a double point:

 "elements": [
    {
        "type": "node",
        "id": 122509567,
        "lat": 50.9772739,
        "lon": 8.0544817,
        "tags": {
            "addr:city": "Hilchenbach",
            "addr:country": "DE",
            "addr:housenumber": "6",
            "addr:postcode": "57271",
            "addr:street": "Bernhard-Weiss-Platz",
            "amenity": "cinema",
            "name": "Viktoria-Kino Dahlbruch",
            "start_date": "1952-05-01",
            "toilets:wheelchair": "yes",
            "wheelchair": "yes"
        }
    },
    {
        "type": "node",
        "id": 269951105,
        "lat": 50.8768216,
        "lon": 8.0214439,
        "tags": {
            "addr:city": "Siegen",
            "addr:country": "DE",
            "addr:housenumber": "31",
            "addr:postcode": "57072",
            "addr:street": "Sandstraße",
            "amenity": "cinema",
            "level": "2",
            "name": "Cinestar",
            "wheelchair": "limited"
        }
    },

My current function looks like this:

async function createCustomCinemaJson (json) {
 try {
     const cinemas = {};

     for (let i = 0; i < json.elements.length; i ++) {
         if(json.elements[i].lat | json.elements[i].tags.addr & json.elements[i].tags.name) {
             console.log(json.elements[i]);
         } else {
             console.log('No Way')
         }
         //cinemas.push(genres[k].name)
     }

     return json

 } catch (e) {

     console.error(e);

 }
}

Currently its no Problem to access "json.elements[i].lat" or "json.elements[i].tags.name" But how cann i access "addr:housenumber" or "addr:city"?

Simon
  • 15
  • 3
  • 1
    `json.elements['addr:housenumber']` ? – Rafael Herscovici Jan 18 '20 at 11:40
  • If it's not a string then it ain't no [JSON](http://json.org) -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Jan 18 '20 at 11:51

1 Answers1

-1

Just use bracket notation with a string as a key name to access such values. Any object value you can access via object["keyname"]

See the example below

var json = JSON.parse(document.getElementById('json').value);
var city = json.elements[0].tags['addr:city'];
console.log(city);

// If you need to iterate the keys because you don't know the name
// you can use for( keyname in object )

var tags = json.elements[0].tags, name;
for(name in tags) {
   if(tags.hasOwnProperty(name)) {
      console.log(name, '=', tags[name]);
   }
}
<textarea id="json">
 {
 "elements": [
    {
        "tags": {
            "addr:city": "Hilchenbach",
            "addr:country": "DE",
            "addr:housenumber": "6",
            "addr:postcode": "57271",
            "addr:street": "Bernhard-Weiss-Platz"
        }
    }
  ]
}
</textarea>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • _"Just use array notation..."_ - It's called ["bracket notation"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors#Bracket_notation) – Andreas Jan 18 '20 at 11:50