how can I go through the next json

1

I have this json and I want to show the value of its keys either by console or by a window.

{ 
    "commerce_brand":"MASTERCARD",
    "commerce_brand":"visa",
    "commerce_brand":"otros"
}
    
asked by javier 30.10.2018 в 15:49
source

2 answers

2

The json as you present it will always throw you "commerce_brand": "others" since you are using the same index or key with different values, so you are always overwriting and the result will be always the last one:

let json = { 
  "commerce_brand":"MASTERCARD",
  "commerce_brand":"visa",
  "commerce_brand":"otros"
}

console.log(json);

I imagine that you want to get several values within commerce_brand , for this commerce_brand it must be an array like this:

let json = { 
  "commerce_brand":["MASTERCARD",
                "visa",
                "otros"]
}

console.log(json);

and you can scroll it like this:

let json = { 
  "commerce_brand":["MASTERCARD",
                    "visa",
                    "otros"]
}

json["commerce_brand"].forEach((elemento, indice) => {
  console.log(elemento);
});

I hope it helps you

    
answered by 30.10.2018 / 16:06
source
4

The specification RFC7159 concerning JSON says that object names should be unique. Although this is only a recommendation it is much more practical if you have to convert it to objects in javascript. Your structure could be converted to manipulate it easily in this way:

 { "commerce_brand": [ "MASTERCARD", "visa", "otros" ] } 
    
answered by 30.10.2018 в 16:17