Trouble Deserializing JSON

I’ve managed to save a JSON file, but I’m not able to read it back. The JSONReader has the following error when debugging:

Invalid Json Token. Line: 1 Ch: 1

FString FullPath = FPaths::GameSavedDir();
FullPath += "filename.json";

FArchive* SaveFile = IFileManager::Get().CreateFileReader(*FullPath);

if (!SaveFile)	return;
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(SaveFile);
//Doesn't work either
//TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(SaveFile);

//JSON Reader has error message after this line
const bool ds = FJsonSerializer::Deserialize(JsonReader, JsonObject); //Returns false
if (JsonObject.IsValid())
//Returns true

Also confirms that the saved JSON is valid.

Any and all help is very much appreciated.


Edit: Here’s the JSON file

        "Name": "a",
        "BC": 0,
        "LN": 121.45,
        "RT": 1.47197,
        "SC": 1
        "Name": "b",
        "BC": 0,
        "LN": 138.924,
        "RT": 1.03696,
        "SC": 1
        "Name": "c",
        "BC": 0,
        "LN": 167.129,
        "RT": 1.38207,
        "SC": 1
        "Name": "d",
        "BC": 1,
        "LN": 195.074,
        "RT": 0.98531,
        "SC": 1
        "Name": "e",
        "BC": 0,
        "LN": 138.697,
        "RT": 0.873527,
        "SC": 1
        "Name": "f",
        "BC": 0,
        "LN": 110.974,
        "RT": 2.22515,
        "SC": 1
        "Name": "g",
        "BC": 0,
        "LN": 120.026,
        "RT": 0.663795,
        "SC": 1

Can you share with us a sample of the contents of the file you are trying to load?

Hi, thanks for replying. Ive updated the original post with the JSON :slight_smile:

That error indicates that it can’t read the [ character, which seems to mean that something went wrong with the character encoding. Is your writer also using TCHAR as the character format? Also, you can check to see if it’s a file issue or a string issue by using the FString version of the reader/writer. If that works but the file one doesn’t, there’s probably something wrong with the file reading part.

The writer I was using looked like this…

  TSharedRef< TJsonWriter > > JsonWriter = TJsonWriterFactory >::Create(&JsonStr)

I also tried this (ignore the printpolicy part, didn’t make a difference either)

TSharedRef< TJsonWriter<TCHAR, TCondensedJsonPrintPolicy<TCHAR> > > JsonWriter = TJsonWriterFactory<TCHAR, TCondensedJsonPrintPolicy<TCHAR> >::Create(&JsonStr);

At first I was using:

*SaveFile << JsonStr;

But it was putting odd characters at the beginning of the file, I thought that to be the problem so tried:

SaveFile->Serialize(TCHAR_TO_UTF8(*JsonStr), JsonStr.Len());

Which god rid of the odd characters but the same error remained.

Now I’m using this which works!

To Write:

TSharedRef< TJsonStringWriter<>> JsonWriter = TJsonStringWriter<>::Create(&JsonStr);
FArchive* SaveFile = IFileManager::Get().CreateFileWriter(*FullPath);
SaveFile->Serialize(TCHAR_TO_UTF8(*JsonStr), JsonStr.Len());
delete SaveFile;

To Read:

FString JsonStr;
FFileHelper::LoadFileToString(JsonStr, *FullPath);
TSharedRef<FJsonStringReader> JsonReader = FJsonStringReader::Create(JsonStr);

FJsonStringReader can’t be found :frowning: i tried to include JsonReader.h but that only gives me dozens of compile errors. what did I miss?

#include “Json.h”
#include “FileManager.h”

in your cpp :slight_smile:

=\ I am at a loss with this one.

I believe the issue may be that it is expecting an object to begin your JSON file, and not an array. This may be causing your issue. It does pass through any validator i pass it into though.

Does anyone why this failed?

// let's ■■■■■■■ test a single json ???
	const FString InputString = "{'version': '1.2.5', 'userName': 'iPhone2Python', 'sessionUUID': '8EF5BA6A-A73E-419A-9BDC-7E18759C180C', 'deviceName': 'iPhone13,4'}";
	TSharedRef< TJsonReader<> > Reader2 = TJsonReaderFactory<>::Create( InputString );

	TSharedPtr<FJsonObject> Object;
	bool bSuccessful = FJsonSerializer::Deserialize(Reader2, Object);
	check( Object.IsValid() );