In Magento 1.9.x. steht die Prototype-Bibliothek inkl. JSON-Validator bereit.
Testet man folgenden String auf „isJSON“, erhält man eine positive Antwort:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"datepicker": { | |
"min":"+7", | |
"max":[2019, 7, 18], | |
"disable":[1,2,[2017,7,18], [2017,8,19],[2020, 12, 5]] | |
}, | |
"timepicker": { | |
"interval": 15 , | |
"min": [9, 0], | |
"max": [16, 0], | |
} | |
} | |
] |
Dabei ist o.g. Beispiel kein valides JSON Format: Hinter einem letzten Element einer Liste darf kein „,“ stehen.
Um sicherer zu validieren, kann man daher folgendes Snippet nutzen:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validation.add('validate-json', 'Please enter valid JSON format', function(v) { | |
try { | |
json = JSON.parse(v); | |
return true | |
} catch (exception) { | |
return false | |
} | |
}); |