Issue in dynamic json array



Hi All,

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.”



This should handle extract the address value from the example you provided regardless if the street has one more lines lines.





What Iam going to suggest is

First take the List of Values of the StreetAddess node

ListA= (From p In obj(“primaryAddress”)(“streetAddress”).Properties
            Select Convert.ToString(p.Value)).ToList

Then Check the Length of the List
If its greater than one then You can directly access the adrress of line 2 by

String Line2 streetAddress=ListA(1).ToString



Wow. This worked perfectly fine. Thanks for this… :slight_smile:

However, now I have another issue in which I have to first check if a particular element may be present or not.

“registeredAddress”: {
“addressCountry”: {
“isoAlpha2Code”: “GB”
"addressLocality": {
** “name”: “GREENFORD”**
** },**
“postalCode”: “UB6 0AZ”

And I have to get the value only when it is present.




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 :slight_smile: