Export tags through SDK

I’m struggling to find a way to export tags through the SDK, in the same fashion as the system.tag.exportTags script function. Is there a straight forward way of doing this? I’m pretty new to all of this.

I can get the TagProvider through the gateway context, and I see that there’s the importTagsAsync method, but no export. I’m trying to export all the datatypes from a given tag provider. My closest guess was something like:

TagPath typesPath = TagPathParser.parse("_types_");
List<TagPath> tagPaths = new ArrayList<TagPath>();
tagPaths.add(typesPath);

CompletableFuture<List<TagConfigurationModel>> cfTagModels = gatewayContext.getTagManager().getTagProvider(providerName).getTagConfigsAsync(tagPaths, true, true);
List<TagConfigurationModel> tModels =cfTagModels.get();

TagConfigurationModel tModel = tModels.iterator().next();

Gson gson = new Gson();
String typesJson = gson.toJson(tModel);

As I was pretty sure of beforehand, this didn’t magically give me the json format I was looking for. Any ideas of how I should be going about exporting tags?

I think you’re close, but create your Gson object like this:

Gson gson = TagGson.create();

where TagGson is com.inductiveautomation.ignition.common.tags.config.TagGson.

Hi Kevin. Makes sense, but I’m only getting empty {} json back now. I did get json data back when using Gson gson = new Gson() that looked like it had all the tags, albeit the wrong format. Am I using toJson on the right type of object here, TagConfigurationModel?

Hmm, looks like you want to do something like this, actually:

JsonObject o = TagUtilties.toJsonObject(model);

Then once you’ve built up an object with either one or multiple models in it you can use Gson::toJson to turn it into a String containing JSON.

1 Like

Thankyou! That seems to to do the trick.