0

I am currently getting information back from a web api in the form of application/json data. The issue I am running into is that sometimes I get an error using JObject.Parse(response) and I don't with JArray.Parse(response), and sometimes it is vice versa. I have been looking around for a bit and haven't been able to figure out why some data returned has [] around it and some does not. That being said, is there a way to parse the data consistently whether it has [] around it or not? The only thing I can currently think is by checking to see if the response string starts with a '[', and use JArray.Parse if it does, and then use JObject.Parse if it doesn't but that seems like a kinda ugly solution. I'm still pretty new to API's in general, so any advice would be appreciate.

(Edit: Language is C#)

Code Snippet:

using (WebClient webClient = new WebClient())
{
    webClient.Headers.Add("Authorization", "Basic ***");
    webClient.Headers.Add("clientId", clientId);
    webClient.Headers.Add("Content-Type", "application/json");
    string agreementResponse = webClient.DownloadString(urlAgreementBase + urlAgreementParams);
    if (response == null)
    {
        throw new InvalidPluginExecutionException("Failed to retrieve company. Response was empty.");
    }
    var agreementObject = JArray.Parse(response);
}

JSON:

[
    {
        "id": int,
        "name": "string",
        "type": {
            "id": int,
            "name": "string",
            "_info": {
                "type_href": "url string"
            }
        },
        "company": {
            "id": int,
            "identifier": "string",
            "name": "string",
            "_info": {
                "company_href": "url string"
            }
        },
        "contact": {
            "id": int,
            "name": "string",
            "_info": {
                "contact_href": "url string"
            }
        },
        "site": {
            "id": int,
            "name": "string",
            "_info": {
                "site_href": "url string"
            }
        },
        "location": {
            "id": int,
            "name": "string",
            "_info": {
                "location_href": "url string"
            }
        },
        "department": {
            "id": int,
            "identifier": "string",
            "name": "string",
            "_info": {
                "department_href": "url string"
            }
        },
        "restrictLocationFlag": bool,
        "restrictDepartmentFlag": bool,
        "startDate": "date string",
        "noEndingDateFlag": bool,
        "cancelledFlag": bool,
        "sla": {
            "id": int,
            "name": "string",
            "_info": {
                "sla_href": "url string"
            }
        }
    }
]
BeamerEA
  • 103
  • 1
  • 7
  • 1
    I would consider it is a backend problem that not a consistent data structure comes from the same API and they should solve it. – Gellio Gao May 19 '21 at 22:11

1 Answers1

0

When I receive data from a server backend, I use JSON.parse(response) I am assuming you are writing in JaveScript. Here is an example I wrote for my webapp;

 $.get("/tictactoe/" + ID, {"thePosition": curr_line}, function(response){
        let x = JSON.parse(response); //here x is now a JSON object
          for(let i = 0; i <= 2; i++){
              for(let j = 0; j <= 4; j++){
                  console.log(x.gb[i][j]); //x holds a 2D-array called gb, 
                  //you can access it with x["gb"] or x.gb then its corresponding elements with [][]
              }
          }
          console.log(x["isValid"]); //isValid is the key, the value is a boolean true/false
        }
    });
NeonFire
  • 186
  • 2
  • 7
  • Sorry, guess it would have been useful to add what language I am using., C#. Thanks for the response, however I don't think C# has a JSON.parse() as far as I am aware. – BeamerEA May 19 '21 at 20:18
  • 1
    https://stackoverflow.com/a/6620173/13328160 try this – NeonFire May 19 '21 at 20:25