Unexpected parsing error from Python server ArgumentException: JSON must represent an object type.

Having a problem trying to parse a Json object returned from my Python server.
The error I am getting is
“ArgumentException: JSON must represent an object type.”

The puzzling thing is if I take the exact string returned from the server and hard code it before passing it to the parsing method Player.CreateFromJSON it works!

These are the 3 relevant lines in the Python server

     x = '{"playerId":8484239823,"playerLoc":"Powai","playerNick":"Random Nick"}'
     y = json.dumps(x)
     socket.send_string(y)

This is the JSON string returned.

    "{\"playerId\":8484239823,\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"}"

This is my Player object taken from the documentation Unity - Scripting API: JsonUtility.FromJson

[System.Serializable]
    public class Player
    {
        public string playerId;
        public string playerLoc;
        public string playerNick;
 
        public static Player CreateFromJSON(string jsonString)
        {
            return JsonUtility.FromJson<Player>(jsonString);
        }
    }

Am using Unity’s native JsonUtility FromJson method as can be seen from above and I simply call

    Player playerInfo = Player.CreateFromJSON(jsonStringFromServer);

to populate the Player object

Have spent countless hours googling and trying to find similar issues. Any feedback or troubleshooting steps welcome!

The escape backslashes are not valid JSON. That might just be how you printed it, not sure.

To see valid JSON, drop any JSON you want into this: https://jsonlint.com

You probably just need a de-escape step.

I think your python code is wrong.

     x = '{"playerId":8484239823,"playerLoc":"Powai","playerNick":"Random Nick"}'
     y = json.dumps(x)
     socket.send_string(y)

You’re taking an already-encoded json string, and then you’re json-encoding that. That’s why your server is producing escape sequences. It’s treating your actual json like a literal string inside a json payload. I think if you just get rid of the json.dumpscall it will work properly. But you will need json.dumps if you are starting not with a string but an actual structured object in Python.