Answer the question
In order to leave comments, you need to log in
How to validate JSON structure (not validation)?
When a JSON response arrives from the server, how can you efficiently check its structure? Those. does it have all the required fields, given the hierarchical structures?
Answer the question
In order to leave comments, you need to log in
Look towards JSON Schema, it seems like it was invented for this ball (http://ru.wikipedia.org/wiki/JSON#JSON_Schema, json-schema.org/ ).
As a starting point:
JSON.isOfPattern = function(json, props){
if (json == null) return false;
for(var key in props){
if (key in json == false) return false;
if (typeof props == 'object' && !JSON.isOfPattern(json[key],props[key])) return false;
}
return true;
}
var json = { name: 'A', address: { city: 'Berlin'}};
JSON.isOfPattern(json, {name:null, address: {city: null} });
If you're sure the JSON came from the right server, why validate it?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question