Convert TArray to json string

Hello all,

This is probably a real simple thing but…

I’m trying to convert an array of strings to JSON string so I can send it over to an api.

TArray items;
items.Add(“Test”);
items.Add(“Test2”);

I have looked at this FJsonObjectConverter | Unreal Engine Documentation
but can’t see anything that supports this. I am not experienced at C++ in any shape or form.

basically, I need this to be returned in a string

[“Test”,“Test”]

Many thanks in advance.

To work with JSON in UE4 You must work with FJsonObject and FJsonValue. For example:

TArray<TSharedPtr<FJsonValue>> items;
items.Add(MakeShareable(new FJsonValueString("Test")));
items.Add(MakeShareable(new FJsonValueString("Test2")));

TSharedRef<FJsonObject> JsonRootObject = MakeShareable(new FJsonObject);
JsonRootObject->SetArrayField("MyArray", items);

FString OutputString;
TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString);
FJsonSerializer::Serialize(JsonRootObject, Writer);

Will produce “MyArray”: [“Test”, “Test2”] json. I don’t know if it is possible to make pure array in ue4 json parser, it always must have a root object.

This will produce a pretty json, if you want to have condensed json use the following writer:

TSharedRef< TJsonWriter<TCHAR, TCondensedJsonPrintPolicy<TCHAR>> > Writer = TJsonWriterFactory<TCHAR, TCondensedJsonPrintPolicy<TCHAR>>::Create(&OutputString);

Other way is to serialize whole structs. For a struct like this:

USTRUCT()
struct FMyStruct
{
	GENERATED_BODY()

	UPROPERTY()
	TArray<FString> Strings;
};

UPROPERTY()
FMyStruct MyStruct;

Filled with:

MyStruct.Strings.Add("Test");
MyStruct.Strings.Add("Test2");

You can run:

TSharedRef<FJsonObject> OutJsonObject = MakeShareable(new FJsonObject);
FJsonObjectConverter::UStructToJsonObject(FMyStruct::StaticStruct(), &MyStruct, OutJsonObject, 0, 0);

FString OutputString;
TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString);
FJsonSerializer::Serialize(OutJsonObject, Writer);

Hope it will help :slight_smile:

Thanks for the help, I get issues trying your second option

‘Member variable declaration’ is not allowed before the Class definition

Any ideas?

#pragma once

#include "Engine.h"

USTRUCT()
struct FQueueTypes {
	GENERATED_BODY()

	UPROPERTY()
	TArray<FString> types;

	FQueueTypes() {}
	

};

UPROPERTY()
FQueueTypes QueueTypes;

It was a mental shortcut from my side. The
UPROPERTY()
FQueueTypes QueueTypes;
is supposed to be inside a class definition. Like for example…

UCLASS()
class ASomeActor : public AActor
{
    GENERATED_BODY()

    UPROPERTY()
    FQueueTypes QueueTypes;

    void BeginPlay() override
    {
        Super::BeginPlay();
        // Do a serialization / deserialization here
    }
};

Thanks very much for your help

For reference, if anybody is interested, of course you can serialize without a root object. I you skip the root object part of the code and directly use the TArray of FJsonValueString objects as an argument for the serialization, it will produce: [“Test”, “Test2”]

The code becomes:

TArray<TSharedPtr<FJsonValue>> items;
items.Add(MakeShareable(new FJsonValueString("Test")));
items.Add(MakeShareable(new FJsonValueString("Test2")));

FString OutputString;
TSharedRef< TJsonWriter<> > Writer = TJsonWriterFactory<>::Create(&OutputString);
FJsonSerializer::Serialize(items, Writer);
1 Like