Array conversion into a json array - JSON.stringify

Problem

I am reading data from a data base: I have the values I want

    for (var index in entities) {                                                                  
    console.log(entities[index].PartitionKey);                                                                    
    console.log(entities[index].RowKey);                                                                       
    console.log(entities[index].Details);                                                               
    console.log(entities[index].Date);
    }

The above will print all the data I want. Now I want to convert this as an Json Object. How can I do that .

I have used and aware that I can use JSON.stringify but when I am trying this here in this context it is giving Error.

I tried keeping in the forloop:

jasonarray[index1].PartitionKey     = entities[index].PartitionKey;
jasonarray[index1].RowKey           = entities[index].RowKey;

and

JSON.stringify({ key: jasonarray[index1].PartitionKey, RowKey: jasonarray[index1].RowKey )

But it is giving as undefined when it comes to executing this function.

All I am looking is var x:

where x is a 

[

{partionkey: "val",key: "val"}
{partionkey: "val",key1: "val"}
{partionkey: "val",key2: "val"}

]
Problem courtesy of: The Learner

Solution

Javascript: working sample http://fiddle.jshell.net/xrB8J/

var entities = [
    {PartitionKey: 'a', RowKey: 5, Details:'details 1', Date:'01.01.2012' },
    {PartitionKey: 'b', RowKey: 7, Details:'details 2', Date:'02.01.2012' },
    {PartitionKey: 'c', RowKey: 3, Details:'details 3', Date:'03.01.2012' }
];

var a = new Array();
for(i = 0; i < 3; i++) 
    a.push({
        PartitionKey: entities[i].PartitionKey, 
        RowKey: entities[i].RowKey
    });

alert(JSON.stringify(a));

‚Äčor c#:

string json = (new JavascriptSerializer()).Serialize(entities.Select(x => new
    {
        x.PartitionKey,
        x.RowKey
    }
));
Solution courtesy of: Boris Gappov

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.