I am trying to deserialize json-array which is an output of 1 RESP-API call that I have made through HTTP-request.
However, this json-array is dynamic:
“primaryAddress”: {
“streetAddress”: {
“line1”: “Technology Hub, SEZ”, “line2”: “Plot No. 3A, Sector - 126”
}
},
So, here line2 may not be present always. And when it is not present, it is throwing an error as : “Write Cell : Object reference not set to an instance of an object.”
deserializedarray.Item(0)(“organization”)(“primaryAddress”)(“streetAddress”)(“line2”).ToString
There are many ways to do it but here are few that I find quite straigthforward
Will return boolean saying if the address locality exists (amend path if necessary) obj.Descendants.Any(Function(x) x.Path.EndsWith("registeredAddress.addressLocality"))
Will return a jtoken if the path exists, otherwise will return nothing (you can then if it is nothing to later access the name amending it with (“name”)) and access its value using .ToString obj.Descendants.LastOrDefault(Function(x) x.Path.EndsWith("registeredAddress.addressLocality"))
Hopefully it helps
Cheers and good WE
PS : the endWith and LastOrDefault have reason behind but cannot afford describing properly now