OL Learn

JSON from datamapper

Is it possible to parse an JSON file from datamapper into workflow variables, with a script ?
It looks like the json file contains a lot of extra info from the datamapper.

JSON sting returned by the datamapper:
[{“schema”:{“columns”:{“TAG_AFB”:“HTMLSTRING”,“ExtraData”:“STRING”}},“id”:27041,“boundaries”:{“start”:0,“end”:3},“datasetid”:4294,“fields”:{“TAG_AFB”:“TAG_AFB:Afbud_frase1”,“ExtraData”:""}}]

I need the data form TAG_AFB parsed to a workflow variable.

Tried the script i normally use, but it is not working - properly cause of the extra data in the json file.

Java script used:
var FileMetaJSON = JSON.parse(Watch.ExpandString(’%c’));
Watch.Log(JSON.stringify(FileMetaJSON), 4);
Watch.SetVariable(‘TADG_AFB’, FileMetaJSON[‘TAG_AFB’]);

Before I just parsed the data from the datamapper in an xml file, it is working but i have to use a xml translater after that to get the danish extra letters to work and that translater fails with conversion errors multiple times a day. So will try the json “Way” if it is posible.

Hi @klaus.soerensen,

Please use the “JSON.stringify()” method for setting the JSON object ‘FileMetaJSON[“TAG_AFB”]’ as the value for the Local Variable “TADG_AFB”, like for example:

Watch.SetVariable("TADG_AFB", JSON.stringify(FileMetaJSON["TAG_AFB"]));

Well it is still the same when i use the JSON.stringify nothing is passed to the variables
I have added the json file.TestData.txt (1.1 KB)
JScript:
var FileMetaJSON = JSON.parse(Watch.ExpandString(’%c’));
Watch.Log(JSON.stringify(FileMetaJSON), 4);

Watch.SetVariable(‘TAG_AFB’, JSON.stringify(FileMetaJSON[‘TAG_AFB’]));
Watch.SetVariable(‘TAG_AFD’, JSON.stringify(FileMetaJSON[‘TAG_AFD’]));
Watch.SetVariable(‘TAG_FOR’, JSON.stringify(FileMetaJSON[‘TAG_FOR’]));
Watch.SetVariable(‘TAG_GUL’, JSON.stringify(FileMetaJSON[‘TAG_GUL’]));
Watch.SetVariable(‘TAG_HEN’, JSON.stringify(FileMetaJSON[‘TAG_HEN’]));
Watch.SetVariable(‘TAG_VBE’, JSON.stringify(FileMetaJSON[‘TAG_VBE’]));
Watch.SetVariable(‘TAG_START’, JSON.stringify(FileMetaJSON[‘TAG_START’]));
Watch.SetVariable(‘TAG_HEADLINE’, JSON.stringify(FileMetaJSON[‘TAG_HEADLINE’]));
Watch.SetVariable(‘TAG_LAWAPPENDIX’, JSON.stringify(FileMetaJSON[‘TAG_LAWAPPENDIX’]));

Let start at the beginning. I’m afraid that JSON.parse(Watch.ExpandString("%c")) doesn’t work because the shared text file contains the BOM characters  at the beginning of the first line. So an option is to find a way to remove these BOM characters from the first line or another option is–if possible–to use a different encoding like UTF-8, instead of UTF-8-BOM.

Are you executing the Run Script plugin directly after the Execute Data Mapping plugin?

Yes it is directly after the datamapper.

The following script strips the BOM header before parsing the file and demonstrates the syntax for accessing any of the fields:

var FileMetaJSON = JSON.parse(Watch.ExpandString('%c').slice(3));

Watch.Log(FileMetaJSON[0].fields['TAG_AFB'],2);