简体   繁体   中英

Issues with JSON formatting for data object in Grafana

Data is not coming in with proper JSON formatting, so I'm having to loop through items in the array to fix the formatting, parsing the changed items and I cannot use the new object(s) when everything is finished because it is no longer in an array. The data is coming in as follows: data [datapoints: [0..1..] target: "up{cluster="bluehills_c3260_cluster",component="atr",datacenter="bluehills",hostname="ny-153-177"...}"] Is there an easier way to convert this using a .map function or some other method to make things cleaner and get the desired result?

I've tried several methods including .replace, .map, and .push. I've also tried JSON.stringify, but nothing else seems to work except what I currently have.

onDataReceived(data) {
  var i;
  for (i = 0; i < data.length; i++) {  // Loop through data array
    var txt = data[i].target;  // Create the variable to store the data target
    var j;
    for (j = 0; j <= txt.length; j++) {  // Loop through the data target
      var newObj = txt.slice(2,j);  // Remove "up"
      var filteredObj = newObj  // Change over to JSON format...
      .replace(/=/g,' : ')
      .replace(/,/g,', ')
      .replace(/{/g,'{ ')
      .replace(/cluster/g,'"cluster"')
      .replace(/component/g,'"component"')
      .replace(/datacenter/g,'"datacenter"')
    }
    var dataObj = filteredObj.replace(/_"cluster"/gi,'_cluster');
    var finalObj = JSON.parse(dataObj);
    console.log("finalObj", dataObj);
  }
}

What I want is a single array with the proper JSON format for the data (target) coming in.

How about this?

const myReg = /([\w\s]+)=\"([^"]*)\"/g
const str = `data [datapoints: [0..1..] target: "up{cluster="bluehills_c3260_cluster",component="atr",datacenter="bluehills",hostname="ny-153-177"...}"]`;
let matches = null;
const resultsJson = {};
while(matches = myReg.exec(str)){
    resultsJson[matches[1]] = matches[2];
}   

{ cluster: 'bluehills_c3260_cluster', component: 'atr', datacenter: 'bluehills', hostname: 'ny-153-177' }

Not sure if this is how you want to have the data stored but that part would be pretty easy to customize.

onDataReceived(data){
    this.createCosmo(data);
  }

  createCosmo(data) {

    var arr = $.map(data,function(value){
      return value.target;
    });

    var arr2 = $.map(arr,function(value){
     var newObj = value.slice(2);  // Remove "up"
     var filteredObj = newObj  // Change over to JSON format
         .replace(/=/g,' : ')
         .replace(/,/g,', ')
         .replace(/{/g,'{ ')
         .replace(/cluster/g,'"cluster"')
         .replace(/component/g,'"component"')
         .replace(/datacenter/g,'"datacenter"')
         .replace(/hostname/g,'"hostname"')
         .replace(/instance/g,'"instance"')
         .replace(/job/g,'"job"')
         .replace(/resilience_group/g,'"resilience_group"')
         .replace(/_"cluster"/gi,'_cluster')
         .replace(/-"cluster"/gi,'-cluster');
         var finalObj = JSON.parse(filteredObj);  // Parse the Object into JSON
         return finalObj;
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM