Issue with Newtonsoft.Json in Unity

Hello Unity community,

I’m encountering a problem while trying to use the Newtonsoft.Json library in Unity. Here’s the sequence of events:

  • I installed the package using NuGet, and it appears correctly in the project references.
  • I don’t receive any errors in Visual Studio, which suggests that the library is correctly referenced there.
  • However, when I switch back to Unity, I get the following error message: error CS0246: The type or namespace name ‘Newtonsoft’ could not be found (are you missing a using directive or an assembly reference?)
  • Interestingly, when I double-click on this error message, it takes me back to Visual Studio, and suddenly Newtonsoft.Json disappears from the references.
  • I manually re-add Newtonsoft.Json to the references in Visual Studio, and the error in Visual Studio disappears. However, the error in Unity persists.
  • Strangely, every time I re-add the reference in Visual Studio, it doesn’t show the little symbol indicating that it needs to be saved. So i attempted to make a small modification to the script in Visual Studio to trigger a save and reload in Unity, but the error CS0246 continues to appear.

I’m puzzled by this behaviour and suspect it could be a bug. Does anyone have any insights or suggestions for resolving this issue?

Thank you for your assistance, and if further clarification is needed, please let me know!

Visual Studio in the context of Unity should just be regarded as a text-editor. Unity more or less handles the Visual Studio solution and generally overrides anything you do to it.

Unity has their own Newtonsoft package you can install via the package manager.

And generally speaking, any package you install via the IDE will not be recognized by the Unity project. Regular NPM packages are installed globally whereas Unity packages or any other asset must be part of the Unity project either located under the Assets tree or installed via Package Manager window.

Nope, totally normal. As Spiney and Smile point out above, it has to be in Unity or it doesn’t count. Unity doesn’t care about Visual Studio. Unity is just as happy if you use vi to write your code.

Here’s my notes on getting JSON .NET in your game:

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as bare arrays, tuples, Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET from the Unity Package Manager (Window → Package Manager).

If you want to avoid the Package Mangler’s UI, just add this to your Packages/manifest.json file:

"com.unity.nuget.newtonsoft-json": "3.0.2",

Also, always be sure to leverage sites like:

https://csharp2json.io

Thank you guys, i really appreciate it! This actually crossed my mind, but since i saw that the Newtonsoft Json.NET is present inside the folder “Packages” in my unity project file i thought i did had to install it with the Unity Package Manager, but i guess that folder is for the scripts and nothing to do with unity. I should have tried that sooner.

I searched in the Unity Package Manager but didn’t find the package there, so i placed a Newtonsoft Json.NET folder, that i downloaded, into the Assets folder in my Unity project and it worked!

Thank you every one once again!

UPDATE
So… that didn’t work, when i reopened the project in unity i add this errors:
“Unloading broken assembly Assets/Plugins/Newtonsoft.Json.dll, this assembly can cause crashes in the runtime”
“Failed to analyze assembly ‘Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’: System.TypeLoadException: Could not resolve type with token 01000016 from typeref (expected class ‘System.Resources.NeutralResourcesLanguageAttribute’ in assembly ‘System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’)”
And if i tried to go to play mode it would just crash.
I did a little search and i found someone that added this “com.unity.nuget.newtonsoft-json”: “3.0.1” to the manifest json file inside the Packages folder of the unity project, and this time i have no errors or warnings.
Still can’t do what i need to do but at least i don’t have errors anymore!

So much for Newtonsoft:

Unfortunately, JSON .NET For Unity is no longer available.

This package has been deprecated from the Asset Store. This means that new purchases of the package are not allowed and that only users who already purchased or downloaded the package before it was deprecated, are allowed to download it.

In most cases, package deprecation happens because the publisher is unable or unwilling to support the package anymore. We suggest looking for alternative packages or contacting the publisher directly.

You can install Unity’s NewtonSoft package by name. Just hit the + → Install Package by Name, and use com.unity.nuget.newtonsoft-json.

And when the Package Manager UI fails to reveal what you search for, just add this line to your Packages/manifest.json file instead:

    "com.unity.nuget.newtonsoft-json": "3.2.1",

That’s for Unity 6.0… for older, like Unity2022, I’m still using this one:

    "com.unity.nuget.newtonsoft-json": "3.0.2",

The whole point of ‘Install Package by Name’ is so you don’t have to manually edit the manifest.json.

… fails, lags, has to be refreshed, blanks, clears the field I was just typing in (that is a CRIMINAL offense in UI terms), just returns nothing… etc.

I don’t want to “play” the package manager like some kind of deranged lootbox minigame hoping it does what I want.

I just want to confidently find and add a package and vi manifest.json lets me do that.

YMMV.

Feels like you’re complaining about issues that haven’t existed for 5+ years.