Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6211

How to safely parse JSON?

$
0
0

I want to parse a JSON from the server and convert it to my model.

Of course, I can use JSON.parse in combination with a model interface, but the problem is that I should also check if the keys of the JSON actually exist and if they match the actual types because, of course, one can't fully trust that what is received as always correct.

How I would go about this is by creating a function that tells if the object is an instance of that particular model where each key would be checked against undefined and also verify its typeof.

How do you usually handle the creation of models in TypeScript?

Thanks!


Viewing all articles
Browse latest Browse all 6211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>