0

my requirement is to draw pie chart for that iam passing json to google chart library like below

script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-json/2.5.1/jquery.json.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
${devicegroupusers}
<c:url value="${UrlRequestMappingConstants.DASHBOARD_DEVICEGROUP_USERS}" var="formUrl"/>

  <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart(devicegroupusers) {

          var a = devicegroupusers, result = [];

        a = JSON.parse(a);

        for (var o = 0; o < a.length; o++) {
            for (var p in a[o]) {
                result.push([p, a[o][p]]);
            }
        };

        console.log(result);

          var data = google.visualization.arrayToDataTable(result);

        var options = {
          title: 'My Daily Activities'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
    </script>
  <div id="piechart" style="width: 900px; height: 500px;"></div>

devicegroupusers contains json like this

[{"name":"Default","count":2},{"name":"IT","count":1},{"name":"R\u0026D","count":1}]

i would like to convert this to

          ['name', 'count'],
          ['deafault',  2],
          ['IT',  3],
          [R\u0026D', 1],

like this please suggest

naveen
  • 31
  • 1
  • 6
  • possible duplicate of [Safely turning a JSON string into an object](http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – Popnoodles Aug 28 '15 at 01:21
  • 1
    Could you show what you've tried? StackOverflow isn't for others to write your code for you. – Chris Aug 28 '15 at 01:28

3 Answers3

0

var array = JSON.parse('[{"name":"Default","count":2},{"name":"IT","count":1},{"name":"R\u0026D","count":1}]').reduce(function(a, b) {
    return Object.keys(b).reduce(function(c, d) {
        return c.push([d, b[d]]) && c;
    }, a);
}, []);
console.log(array);
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

var a = '[{"name":"Default","count":2},{"name":"IT","count":1},{"name":"R\u0026D","count":1}]',
    result = [];

a = JSON.parse(a);

for (var o = 0; o < a.length; o++) {
    for (var p in a[o]) {
        result.push([p, a[o][p]]);
    }
};

console.log(result);
Vidul
  • 10,128
  • 2
  • 18
  • 20
0

Your proposed output is a bit weird (e.g. 'Default' is in quotation marks, but IT is not), but here are two close approximations using bash and jq:

$ jq -c '.[] | to_entries[] | [.key, .value]' input.json
["name","Default"]
["count",2]
["name","IT"]
["count",1]
["name","R&D"]
["count",1]

$ jq -cr $'def n: if type == "number" then . else "\'\(.)\'" end;
  .[] | to_entries[] | "[\'\(.key)\', \(.value|n)]"' input.json
['name', 'Default']
['count', 2]
['name', 'IT']
['count', 1]
['name', 'R&D']
['count', 1]
peak
  • 105,803
  • 17
  • 152
  • 177