I have a problem with a JSON, the problem is that I am receiving a technical sheet from a JSON. The problem is not to consult the information, the problem I have is that it is not always the same information in the JSON, let me explain, in some technical sheets the first two fields can be Product type and Number of labels per roll , but in others technical sheets the first fields are Type of product and the second is manageable
The problem is that for example if I wanted to do the following:
$product = $array['technical_sheet'][labels_per_roll];
Sometimes it would give me an error because some technical sheets do not have this field and from here my doubt arises if it is possible that instead of processing the json I can print it with an HTML format?
I attach some images of the differences between the json that I can receive in the technical sheets:
You could use loops
foreach
to iterate through the JSON . The nice thing about these loops is that you don't have to know the indices to be able to iterate through an object or an array .In my example I first use a loop
foreach
to iterate through each object in the array and a second loop to iterate through each property of the object printing both its key and its value .Departure:
In this way, although the objects have different properties, it would not matter to us because you will be able to capture the value of each one of them.
Hello, you could do it if the variable exists or not, try this:
Oh do it through AJAX
but what is the problem if it has it or not?, since you access the object via
key o indice
, in case you have different views for each one you can try withficha_tecnica.hasOwnProperty("etiquetas_por_rollo")
although according to you youJSON
would like something like thisIn case of being a single view you can validate it in this way