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
-
Can you confirm your JSON structure? as it is now, it is invalid.user400654– user4006542013年03月07日 19:10:04 +00:00Commented Mar 7, 2013 at 19:10
-
1Your JSON is not valid. What have you tried to do?Explosion Pills– Explosion Pills2013年03月07日 19:10:36 +00:00Commented 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.bfavaretto– bfavaretto2013年03月07日 19:12:15 +00:00Commented Mar 7, 2013 at 19:12
-
jsonlint.com can help you validate your jsonSanketh Katta– Sanketh Katta2013年03月07日 19:14:01 +00:00Commented Mar 7, 2013 at 19:14
-
1possible duplicate of create a new JSON, from a existing JSON response - please do not delete and recreate your questions, but edit them.Bergi– Bergi2013年03月07日 19:17:52 +00:00Commented Mar 7, 2013 at 19:17
2 Answers 2
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
Bergi
671k162 gold badges1k silver badges1.5k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Bergi
@MikeRobinson:
in is really fine. hasOwnProperty used like in your edit has similar problems.Mike Robinson
After I edited that I did some more reading. Right you are. Nice solution too.
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
Ghost
4631 gold badge4 silver badges5 bronze badges
2 Comments
Bergi
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.Ghost
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.
lang-js