No Results
How to Parse Raw JSON Data

If you have a JSON file with data in key-value pair, you can parse it to get the value of any data attribute by using custom JavaScript.

Our JSON file, which you can download here, looks like this:

We have created a Server Filesystem connection and then a Custom feed off of it.

In the Custom feed, we have uploaded the above-mentioned JSON file, as you can see in this video.

This is the script we have used in the Script tab (downloadable here, so you can copy it easily):

function getAttributes(inputString, nodeVars, secVars) {
  // TODO: Create an array of attribute objects following the sample below
  // use inputString to derive attributes
  var attributes = [];
 
    attributes.push({
      name : 'recordedAt', // Alphanumerics and underscores only; no leading, trailing, or adjacent underscores
      type : 'date', // 'string'|'int'|'long'|'number'|'boolean'|'date'
      isId : false, // true|false
      units : 'seconds', // 'millis'|'seconds' (if attribute is a timestamp)
    });
    attributes.push({
      name : 'avg', // Alphanumerics and underscores only; no leading, trailing, or adjacent underscores
      type : 'number', // 'string'|'int'|'long'|'number'|'boolean'|'date'
    });
    attributes.push({
      name : 'max', // Alphanumerics and underscores only; no leading, trailing, or adjacent underscores
      type : 'number', // 'string'|'int'|'long'|'number'|'boolean'|'date'
    });
    attributes.push({
      name : 'min', // Alphanumerics and underscores only; no leading, trailing, or adjacent underscores
      type : 'number', // 'string'|'int'|'long'|'number'|'boolean'|'date'
    });
 
  return jsAttributesSuccess(attributes, "Example Success");
  // can optionally return jsAttributesFailure(failureMsg) to handle failures
}

function getObjectKeys(obj) {
    return Object.keys(obj);
}

function getRecords(inputString, nodeVars, secVars) {
  //logger.info(JSON.parse(inputString));
 
  var rawJsonData = JSON.parse(inputString);
  //logger.info(rawJsonData.data["0"]);
 
  //entry point
  var parsedRawData = rawJsonData.data['0'];
  var records = [];
  var lookup = {};
  var sampleRecord;
  var key;
 
  var avgData = parsedRawData.avg;
  var maxData = parsedRawData.max;
  var minData = parsedRawData.min;
 
  var avgDataKeys = getObjectKeys(avgData);
  var maxDataKeys = getObjectKeys(maxData);
  var minDataKeys = getObjectKeys(minData);
 
  avgDataKeys.forEach(function(key) {
    // logger.info(key);
    // logger.info(sampleRecord);
    sampleRecord = {};
    //sampleRecord = { 'recordedAt': key, 'avg': avgData[key] };
    //sampleRecord.recordedAt = key;
    //sampleRecord.avg = avgData[key];
    sampleRecord['recordedAt'] = key;
    sampleRecord['avg'] = avgData[key];
    lookup[key] = sampleRecord;
    records.push(sampleRecord);
  });
 
  maxDataKeys.forEach(function(key) {
    lookup[key]['max'] = maxData[key];
  });
 
  maxDataKeys.forEach(function(key) {
    lookup[key]['min'] = minData[key];
  });
 
  //logger.info(JSON.stringify(records));
  if (records) {
    return jsRecordsSuccess(records, "Example Success");
  } else {
      return jsRecordsFailure(records, "Failed to push data to records");
  }
 
  // can optionally return jsRecordsFailure(failureMsg) to handle failures
}

This is how the JSON is parsed in the preview:


Terms | Privacy