Before trying to get the value, you can use isNull
to verify if the value is null or does not exist.
Example:
JSONObject obj = jArray.getJSONObject(id);
if (!obj.isNull("duration")) {
valor = obj.getInt("duration");
}
If you need to know if the value is indeed null (it exists but it is null), it is a bit more complicated.
if(obj.has("duration") && obj.isNull("duration")) {
// el valor existe, pero es null.
}
As usual, null I could have a meaning tristate , you will have to consider each case before making your decision.
In addition, the previous answer already says, it is also optInt
(and optString, optLong, etc), but this method will give you the default value, even if the field was not set at all. So you should opt for one or another option according to the needs of the case.