4

I now have a JSON response from the server like, but i want to restructure it, according to dates. for e.g i now have

{
"items":
 [
 {
 "A": [
 {
 "name": "a",
 "date": "2/10/2010",
 "sales": "100"
 },
 {
 "name": "b",
 "date": "6/10/2010",
 "sales": "400"
 }
 ],
 "B": [
 {
 "name": "c",
 "date": "2/10/2010",
 "sales": "1000"
 },
 {
 "name": "d",
 "date": "6/10/2010",
 "sales": "400"
 }
 ]
 },
 {
 "C": [
 {
 "name": "a",
 "date": "10/10/2010",
 "sales": "100"
 },
 {
 "name": "b",
 "date": "6/10/2010",
 "sales": "100"
 }
 ],
 "D": [
 {
 "name": "c",
 "date": "2/10/2010",
 "sales": "300"
 },
 {
 "name": "c",
 "date": "2/10/2010",
 "sales": "1100"
 }
 ]
 }
 ]
}

but now i want to restructure by dates, for a particular day need to know all the sales to have like,

 {
 "date1" : [sales, sales, sales],
 "date2" : [sales, sales],
 "date3" : [sales]
 }

how can this be done?

iambriansreed
22.3k6 gold badges66 silver badges81 bronze badges
asked Mar 7, 2013 at 19:09
7
  • Can you confirm your JSON structure? as it is now, it is invalid. Commented Mar 7, 2013 at 19:10
  • 1
    Your JSON is not valid. What have you tried to do? Commented Mar 7, 2013 at 19:10
  • Are you parsing the JSON manually, or letting jQuery parse it for you? If manually, you could use a reviver function. Commented Mar 7, 2013 at 19:12
  • jsonlint.com can help you validate your json Commented Mar 7, 2013 at 19:14
  • 1
    possible duplicate of create a new JSON, from a existing JSON response - please do not delete and recreate your questions, but edit them. Commented Mar 7, 2013 at 19:17

2 Answers 2

4

I can only assume that you want this:

var arr = JSON.parse(response); // or something like that (assuming valid JSON)
 // or just assign the array literal
var result = {};
for (var i=0; i<arr.length; i++) {
 var date = arr[i].date,
 sales = arr[i].sales;
 if (date in result)
 result[date].push(sales);
 else
 result[date] = [sales];
}
answered Mar 7, 2013 at 19:15
Sign up to request clarification or add additional context in comments.

2 Comments

@MikeRobinson: in is really fine. hasOwnProperty used like in your edit has similar problems.
After I edited that I did some more reading. Right you are. Nice solution too.
0

Assuming you get your JSON worked out:

<?php
$list = array(
 array(
 'product' => 'a',
 'date' => '2012-08-01',
 'sales' => 500,
 ),
 array(
 'product' => 'b',
 'date' => '2012-08-02',
 'sales' => 600,
 ),
 array(
 'product' => 'c',
 'date' => '2012-08-01',
 'sales' => 250,
 ),
 array(
 'product' => 'd',
 'date' => '2012-08-02',
 'sales' => 800,
 ),
);
echo json_encode($list);
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
var JSON = '<?php echo json_encode($list); ?>';
var pJson = jQuery.parseJSON(JSON);
var cArray = new Array();
for each ( i in pJson ) {
 if ( typeof cArray[i.date] == 'undefined' ) {
 cArray[i.date] = Array();
 }
 cArray[i.date].push(i.sales);
}
</script>

I added the php section to show what a JSON string should look like. This solution also requires jQuery.

answered Mar 7, 2013 at 19:42

2 Comments

There is no reason to output JSON as strings into scripts, you should just output Object literals. And your way is very error-prone, as you forgot to escape the JSON string. Try it with only one JSON value that includes a " or ' and your solution blows up.
I understand Bergi, the PHP and outputting the JSON was simply so he/she could see a valid JSON string. I mentioned this is ASSUMING he had his/her JSON string worked out. Sorry for posting after your answer, I did not refresh the page.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.