Why UnityWebRequest.Post results with "JSON parse error: Missing a name for object member?"

Hi everybody
I’m experiencing an error ArgumentException: JSON parse error: Missing a name for object member. when using the method:

IEnumerator PostJob(string url, string json)
    {
        var postJob = new UnityWebRequest(url, "POST");
        byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
        postJob.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
        postJob.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

Specifically what I see is, while posting json that looks like this (dummy json for example, but notice the “” vs ’ ') :

{“title”:“”,“item_id”:“”,“item_name”:“”}

in return, from postJob.doanloadhandler.text, I get a json that looks like this:

{‘title’: ‘ice_cream’, ‘item_id’: ‘ff8rj9rje’, ‘item_name’: ‘Cornetto’}

  1. I read somewhere that json with ’ ’ (instead of " " ) is invalid, is that correct?

  2. Why would an invalid json be returned?

  3. Is that an issue that can be resolved with another json parser?

  4. Why do I get the Missing a name for object member? It looks to me that the names for the sent and the received json are the same (I’ve tripled checked on the original json as well they are the same).

The code you posted doesn’t do any JSON parsing, the error should be coming from somewhere else. Show the actual line of the error please, as well as the code for any C# class or struct that you’re deserializing into.

1 Like

Find out for yourself! You can test any JSON you like right here:

https://jsonlint.com

And don’t forget:

https://json2csharp.com
https://csharp2json.io

A bug, a bad request, a bad server, bad load balancer, bad conversion, bad hackers intercepting your data, bad just about anything in the 57,000 things standing between request and response.

As Praetor points out, there’s no JSON parser above. Are these errors coming from your server?

Also, as I posted in the other thread, you can speculate indefinitely about data, or you can go observe it directly:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

https://discussions.unity.com/t/831681/2

https://discussions.unity.com/t/837672/2

And setting up a proxy can be very helpful too, in order to compare traffic:

https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

1 Like

This is the post method I sent:

            var request = new UnityWebRequest(url, "POST");
            byte[] jsonToSend = new UTF8Encoding().GetBytes(bodyJsonString);
            request.uploadHandler = new UploadHandlerRaw(jsonToSend);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");


            yield return request.SendWebRequest();

            if (request.isHttpError || request.isNetworkError)
            {
                Debug.Log(request.error);
            }
          
            var result = request.downloadHandler.text;
            var jsonData = JsonUtility.FromJson<Bearer>(result);

            //Do something with jsonData...

This is how the Bearer.cs class looks like:

    [System.Serializable]
    public class Bearer
    {
        public string title;
        public string item_id;
        public string item_name;
     }

The Bearer.cs is the same class I use as jsonBody with:

private void RequestPost(){
var bearer = new Bearer();
var jsonBody = JsonUtility.ToJson(bearer);
StartCoroutine(PostJob(URL, jsonBody));
}

Maybe the issue starts with the way the jsonbody string is serialized?

Are you storing the data into the Bearer class? I don’t see that in your code anywhere.

EDIT: Oh I saw in your other post you got a return.

To 1. Yes, that’s correct. what you got there is not valid json. Json is a subset of the javascript language and denotes just the JavaScript Object Notation, however a lot stripped down. This page describes the entirety of JSON.

To 2. Why the server you contact returns something that is not valid json? How should we know that? Is that server supposed to return json? If so, it’s a bug on the server side. Though technically a server could return whatever data it wants. That depends on your server.

To 3. Technically that could be possible, though most parsers would not accept such formatted json. So unless the server API really can not be changed / fixed I would not consider such an approach.

To 4. This is already explained by point number 1. The returned text is not valid JSON. It does not recognise your ‘key’ as a “key” because valid keys have to be put in double quotes.

Thanks for your reply - I got the url from a third party and they didn’t know they return a broken JSON.

Well, if you can’t change it, you could try simply replacing the quotes before parsing.

string json = postJob.downloadHandler.text.Replace('\'','"');

This may work but could also introduce other issues depending on the content of your json.

1 Like

Thanks for this tip :slight_smile: