For custom channels, EasyCatalog allows the creation of scripts that take channel data and turn into a record set which EasyCatalog can load. Scripts are written in Lua. They take raw channel JSON and return a RECORDSET object. The name of the file should match the name of the channel, with a.lua extension.
Scripts should be placed inside the EasyCatalog Workspace->Scripts->Salsify folder.
Example script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | -- digest JSON, JSONSOURCE is passed to the script json_return = cjson.decode(JSONSOURCE); unique_field_names = {}; unique_field_count = 0; -- set up key name keyFieldName = "part_id"; -- initialise fields and records arrays. fields = { { name = keyFieldName, key = "true"} }; records = {}; unique_field_names[keyFieldName] = 1; unique_field_count = 1; -- process each record for r=1, #json_return, 1 do record = json_return[r]; -- obtain/construct key key = record[keyFieldName]; keyStr = ""; for k=1, #key, 1 do if (k > 1) then keyStr = keyStr .. '\r'; end keyStr = keyStr .. key[k]; end -- create a new record newrecord = { keyStr } -- sort by key key = {}; value = {}; for a,b in pairs(record) do table.insert(key,a); table.insert(value,b); end table.sort(key); -- process each field for a, b in pairs(key) do fieldName = b; val = record[b]; -- build list of unique field names if unique_field_names[fieldName] ~= 1 then unique_field_names[fieldName] = 1; unique_field_count = unique_field_count + 1; fields[unique_field_count] = { name = fieldName }; end -- obtain/construct value valueStr = ""; for a=1, #val, 1 do if (a > 1) then valueStr = valueStr .. '\r'; end valueStr = valueStr .. val[a]; end -- set field name and value. newrecord[fieldName] = valueStr; end -- insert record table.insert(records, newrecord); end -- return a new RECORDSET return RECORDSET.new(fields, records); |