How to build this specific JSON Format with the given input

Pawan

I am receiving the following JSON from back end

Right now there are two strike prices 6000 and 7000 and CE and PE open interest values for each strike price

[{
    "CE": "601650",
    "strike_pr": "6000",
    "PE": "1196350"
}, {
    "CE": "2553975",
    "strike_pr": "7000",
    "PE": "6069025"
}]

I am trying to generate a google visulization bar chart with this values as shown in this fiddle

http://jsfiddle.net/7wYP2/453/

Could you please how can i build below format JSON Array form the above JSON

var data = google.visualization.arrayToDataTable([
        ['STRIKE_PR', 'CE', 'PE'],
        ['6000  ',  601650,   1196350],
          ['7000',  2553975,   6069025]
    ]);
Rajesh

You can try something like this:

Note: This answer assumes all objects will have same properties.

@Dandavis's suggestion

var data = [{
    "CE": "601650",
    "strike_pr": "6000",
    "PE": "1196350"
}, {
    "PE": "6069025",
    "CE": "2553975",
    "strike_pr": "7000",
}];

var keys = Object.keys(data[0]);

var result = data.map(function(o){
  return keys.map(function(k){
    return o[k];
  });
});

result.unshift(keys);

document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");

Array.forEach + Array.push

var data = [{
    "CE": "601650",
    "strike_pr": "6000",
    "PE": "1196350"
}, {
    "CE": "2553975",
    "strike_pr": "7000",
    "PE": "6069025"
}];

var keys = Object.keys(data[0]);

var result = [];
result.push(keys);

data.forEach(function(o){
  var _tmp = [];
  keys.forEach(function(k){
    _tmp.push(o[k])
  });
  result.push(_tmp);
});

document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");

Array.map + Array.unshift

var data = [{
    "CE": "601650",
    "strike_pr": "6000",
    "PE": "1196350"
}, {
    "CE": "2553975",
    "strike_pr": "7000",
    "PE": "6069025"
}];

var keys = Object.keys(data[0]);

var result = data.map(function(o){
  var _tmp = [];
  keys.forEach(function(k){
    _tmp.push(o[k])
  });
  return _tmp;
});

result.unshift(keys);

document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to read specific input format

From Dev

how to create a dataframe to generate json in the given format

From Dev

How to print a given array in JSON format?

From Dev

How to check if input string matches a specific format?

From Dev

how to order the table by sorting a given column with some specific format

From Dev

How to take a value of the input given in a file into an array with specified format

From Dev

How to Build the required JSON Format in ios?

From Dev

How to build the body of a request in json format

From Dev

How does `go build` decide which subdirectories to build when it is not given specific packages to build?

From Dev

jQuery to format input in a specific format

From Dev

Input in specific format (matrix)

From Dev

How to format Highcharts to get specific json data?

From Dev

How to Convert Time to Json Format with user given time?

From Dev

How to Convert Time to Json Format with user given time?

From Dev

How to input a date with a specific date format in struts 2.2.3.1?

From Dev

Format a date given from a json

From Dev

Creating a specific JSON format

From Dev

Create JSON in specific format

From Dev

Reset input file if given file format is wrong

From Dev

How to read a Json file with a specific format with Spark Scala?

From Dev

How do I write a json file in a specific format?

From Dev

How to format Serilog JSON output in a very specific manner

From Dev

How to convert specific CSV format to JSON using Python

From Dev

how to format the text-file data to a specific standard JSON

From Dev

Laravel (Request $request) handling how to check if input is in the correct json format?

From Dev

Read input from input file given in specified format in scala

From Dev

How to get a specific given in Firebase

From Dev

How to ignore a given console input

From Dev

ColdFusion JSON return in a specific format

Related Related

  1. 1

    how to read specific input format

  2. 2

    how to create a dataframe to generate json in the given format

  3. 3

    How to print a given array in JSON format?

  4. 4

    How to check if input string matches a specific format?

  5. 5

    how to order the table by sorting a given column with some specific format

  6. 6

    How to take a value of the input given in a file into an array with specified format

  7. 7

    How to Build the required JSON Format in ios?

  8. 8

    How to build the body of a request in json format

  9. 9

    How does `go build` decide which subdirectories to build when it is not given specific packages to build?

  10. 10

    jQuery to format input in a specific format

  11. 11

    Input in specific format (matrix)

  12. 12

    How to format Highcharts to get specific json data?

  13. 13

    How to Convert Time to Json Format with user given time?

  14. 14

    How to Convert Time to Json Format with user given time?

  15. 15

    How to input a date with a specific date format in struts 2.2.3.1?

  16. 16

    Format a date given from a json

  17. 17

    Creating a specific JSON format

  18. 18

    Create JSON in specific format

  19. 19

    Reset input file if given file format is wrong

  20. 20

    How to read a Json file with a specific format with Spark Scala?

  21. 21

    How do I write a json file in a specific format?

  22. 22

    How to format Serilog JSON output in a very specific manner

  23. 23

    How to convert specific CSV format to JSON using Python

  24. 24

    how to format the text-file data to a specific standard JSON

  25. 25

    Laravel (Request $request) handling how to check if input is in the correct json format?

  26. 26

    Read input from input file given in specified format in scala

  27. 27

    How to get a specific given in Firebase

  28. 28

    How to ignore a given console input

  29. 29

    ColdFusion JSON return in a specific format

HotTag

Archive