INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Rearranging a Json object

Rearranging a Json object

(OP)
I am trying to convert a json object that contains:

CODE

var dataold = [{
    "DateGroup": "05/15/2012",
    "Product": "Var1",
    "Duration": 0
  },{
    "DateGroup": "05/15/2012",
    "Product": "Var2",
    "Duration": 53.999700
  },{
    "DateGroup": "05/15/2012",
    "Product": "Var3",
    "Duration": 86.999580
  }
]; 

to this format:


CODE

var datanew = [{
    "DateGroup": "05/15/2012",
    "var1": 0,
    "var2": 53.999700,
    "var3": 86.999580
  },{
    "DateGroup": "06/17/2012",
    "var1": 32.999880,
    "var2": 59.999700,
    "var2": 59.999700
  },{
    "DateGroup": "07/19/2012",
    "var1": 37.999880,
    "var2": 0,
    "var3": 83.999580
  }
]; 

but am not sure how to go about parsing the object and making the necessary changes.

RE: Rearranging a Json object

it will probably be easier to do this in whatever server side software you use to generate the input. If you must do it in javascript then you will need to constuct some nested loops or tweak your structure a little bit (in which case you can probably get it done in a single loop).

RE: Rearranging a Json object

(OP)
I need to do it in Javascript. I've been trying to do it with nested loops, but without much success.

RE: Rearranging a Json object

i have not tested it but something like this might work. it's not exactly the same output format, but close.

CODE

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var nO = {};
var oO = [{
    DateGroup: "05/15/2012",
    Product: "Var1",
    Duration: 0
  },{
    DateGroup: "05/15/2012",
    Product: "Var2",
    Duration: 53.999700
  },{
    DateGroup: "05/15/2012",
    Product: "Var3",
    Duration: 86.999580
  }
]; 
for(var i = 0; i < oO.length; i++){ 
for(var i = 0; i < oldO.length; i++){ 
 if(!(oldO[i].DateGroup in nO))nO[oldO[i].DateGroup] = {};
 nO[oldO[i].DateGroup][oldO[i].Product] = oldO[i].Duration;
}
document.getElementById('demo').innerHTML = JSON.stringify(nO);
</script>
</body>
</html> 


RE: Rearranging a Json object

and just in case you don't know how to retrieve the data

CODE

for(var dateGroup in nO){
 for(var Property in nO[DateGroup]){
   var Duration = nO[DateGroup][Property];
   // you now have the dategroup, the property and the duration for that property
 }
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close