3

I'm developing a chart using highchart plugin. For giving dynamic input for chart. So, I need to change the JSON format to Array. how to change the JSON array object format to Array using javascript?

[
        {
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        },
        {
            "scoreCount": "20",
            "legendDesc": "Fail ",
            "colorPatchCode": "#FF0000"
        }
    ]

to convert Array format like ,

[
        [
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        ],
        [
            "scoreCount": "20",
            "legendDesc": "Fail ",
            "colorPatchCode": "#FF0000"
        ]
    ]

Can any one help?

9
  • 2
    Second snippet is not valid JavaScript. Commented Oct 25, 2012 at 12:54
  • arrays in javascript are not key-value pairs, they are just values. this is not possible. what are you trying to do with this structure that the array of objects can't provide? Commented Oct 25, 2012 at 12:55
  • but i need the output like that way. Commented Oct 25, 2012 at 12:55
  • @selladurai the output CANT be like this. please supply the goal and well try to help. Commented Oct 25, 2012 at 12:56
  • Why do you need it that way? What are you trying to accomplish that requires that format? Commented Oct 25, 2012 at 12:57

5 Answers 5

4
[
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        ],

is not valid structure. Array can contain single element. native , or object which contain another items(properties) , or array.

Sign up to request clarification or add additional context in comments.

3 Comments

my friend , As a developer we should write valid code . ofcourse we can create a string with invalid data but it wont be interoperable. Dont think as of right now . think how can you make it the right way. im sure the api youre using doesnt enforce you to write this invalid code.
[[1,2],[3,4]] is very different from [[1:2],[3:4]]
1

What if try this:

var arr = [
        {
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        },
        {
            "scoreCount": "62",
            "legendDesc": "Good ",
            "colorPatchCode": "#99CC00"
        },
        {
            "scoreCount": "55",
            "legendDesc": "Acceptable ",
            "colorPatchCode": "#FFFF00"
        },
        {
            "scoreCount": "31",
            "legendDesc": "Poor ",
            "colorPatchCode": "#FF9900"
        },
        {
            "scoreCount": "20",
            "legendDesc": "Fail ",
            "colorPatchCode": "#FF0000"
        }
    ];

for (i = 0;i<arr.length;i++) {
    var a = [];
    for (n in arr[i]) {
        a[n] = arr[i][n];
    }
    arr[i] = a;
}

for (i = 0;i<arr.length;i++) {
    alert(arr[i]['scoreCount']);
}

Comments

0

http://jsfiddle.net/sPN7z/

var source = [
    {
        "scoreCount": "108",
        "legendDesc": "Excellent ",
        "colorPatchCode": "#009900"
    },
    {
        "scoreCount": "62",
        "legendDesc": "Good ",
        "colorPatchCode": "#99CC00"
    },
    {
        "scoreCount": "55",
        "legendDesc": "Acceptable ",
        "colorPatchCode": "#FFFF00"
    },
    {
        "scoreCount": "31",
        "legendDesc": "Poor ",
        "colorPatchCode": "#FF9900"
    },
    {
        "scoreCount": "20",
        "legendDesc": "Fail ",
        "colorPatchCode": "#FF0000"
    }
];

var out = [];

for (var i = 0; i < source.length; i++) {
    var tmp = [];
    for (var i2 in source[i]) {
        tmp.push(source[i][i2]);
    }
    out.push(tmp);
}

out would then be:

[
    ["108","Excellent ","#009900"],
    ["62","Good ","#99CC00"],
    ["55","Acceptable ","#FFFF00"],
    ["31","Poor ","#FF9900"],
    ["20","Fail ","#FF0000"]
]

as the others said already.. you cannot store key values in javascript arrays. thats what json is for.

Comments

0

Though the syntax is wrong, but if you want you can make this format of string anyway.

But, as you know, you cannot read it back to JavaScript variable.

Comments

0

You can use object oriented capabilities of javascript to accomplish this in more cleaner way.

var jsonArray = [
   {
     "scoreCount": "108",
     "legendDesc": "Excellent ",
     "colorPatchCode": "#009900"
},
{
     "scoreCount": "62",
     "legendDesc": "Good ",
     "colorPatchCode": "#99CC00"
},
{
     "scoreCount": "55",
     "legendDesc": "Acceptable ",
     "colorPatchCode": "#FFFF00"
},
{
     "scoreCount": "31",
     "legendDesc": "Poor ",
     "colorPatchCode": "#FF9900"
},
{
     "scoreCount": "20",
     "legendDesc": "Fail ",
     "colorPatchCode": "#FF0000"
}
];

function MyJsObject(scoreCount, legendDesc, colorPatchCode) {
     this.scoreCount = scoreCount;
     this.legendDesc = legendDesc;
     this.colorPatchCode = colorPatchCode;
}

function createJSArray() {
     var jsArray = new Array();
     for(i = 0; i < jsonArray.length; i++) {
         var m = new MyJsObject(jsonArray[i].scoreCount,                         jsonArray[i].legendDesc, jsonArray[i].colorPatchCode);
         jsArray[i] = m;
     }
     alert(jsArray[4].scoreCount);//access any object like this
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.