简体   繁体   English

Grafana中数据对象的JSON格式问题

[英]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. 数据未采用正确的JSON格式输入,因此我必须遍历数组中的项以修复格式,解析更改后的项,并且一切都完成后,我将无法使用新对象,因为它不再在一个数组中。 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? 数据输入如下:数据[数据点:[0..1 ..]目标:“ up {cluster =” bluehills_c3260_cluster“,component =” atr“,datacenter =” bluehills“,主机名=” ny-153- 177“ ...}”]有没有更简单的方法可以使用.map函数或其他方法将其转换为更整洁的方法并获得所需的结果?

I've tried several methods including .replace, .map, and .push. 我尝试了几种方法,包括.replace,.map和.push。 I've also tried JSON.stringify, but nothing else seems to work except what I currently have. 我也尝试过JSON.stringify,但是除了我目前拥有的东西以外,其他任何东西似乎都无法正常工作。

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. 我想要的是一个具有正确JSON格式的数组,用于输入的数据(目标)。

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' } {丛集:'bluehills_c3260_cluster',组件:'atr',数据中心:'bluehills',主机名:'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;
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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