1

What is the best way to restructure Json with Angularjs or Jquery..

I am getting a json with following format

    [
  {
     "address":"balh",
     "lat":123,
     "long":456
  },
  {
     "address":"balh",
     "lat":321,
     "long":543
  },
  {
     "address":"balh",
     "lat":432,
     "long":333
  },
  {
     "address":"balh",
     "lat":123,
     "long":456
  },
  {
     "address":"balh",
     "lat":321,
     "long":543
  },
  {
     "address":"balh",
     "lat":432,
     "long":333
  }
]

The format I need is to group it with three like following..

    {
   "1":[
      {
         "address":"balh",
         "lat":123,
         "long":456
      },
      {
         "address":"balh",
         "lat":321,
         "long":543
      },
      {
         "address":"balh",
         "lat":432,
         "long":333
      }
   ],
   "2":[
      {
         "address":"balh",
         "lat":123,
         "long":456
      },
      {
         "address":"balh",
         "lat":321,
         "long":543
      },
      {
         "address":"balh",
         "lat":432,
         "long":333
      }
   ]
}
4
  • 3
    Your input JSON is not actually valid JSON: the outermost { and } should be [ and ] because it is an array. Assuming you corrected this, you wouldn't restructure the JSON string, you'd parse it and then manipulate the resulting object to create a new object with the structure you need (which you could then serialise back to a JSON string if you needed to). Commented Dec 4, 2012 at 6:03
  • Thanks nnnnnn, I don't think that you looked at Angularjs.. There is a reason to do so to use direct json binding with ng-repeat.. Commented Dec 4, 2012 at 6:07
  • I don't understand what you mean about AngularJS. My point was that JSON is always a string, and doing string manipulation to change the format is clunky and tedious. The "correct" solution is to parse the JSON so that you are dealing with objects. If your input is already an object rather than a string then it is not JSON. Commented Dec 4, 2012 at 6:27
  • Thanks.. You are totally correct.. I mean that AngularJS framework does all json parsing and iteration.. I just wanted to use the framework as it is providing the format I need to map.. Commented Dec 4, 2012 at 6:41

1 Answer 1

2

This seems more like a straight JavaScript question to me.

The code below should take your array and break it into a dictionary of arrays with three (or less) objects each:

var yourArray = [/*whatever it is in here*/];

var output = {};

for(var i = 0, c = 1, current = null; i < yourArray.length; i++) {
    if(i % 3 == 0) {
        output[c++] = current = [];
    }
    current.push(yourArray[i]);
}

Here's a fiddle (using Angular) demonstrating before and after output

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

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.