-1

I have a json object and I need to nest it so I can work with it much better.

my json looks as follow:

[
  {
    "lat": null,
    "lng": null,
    "locationId": null
  },{
    "lat": null,
    "lng": null,
    "locationId": null
  }
]

and I need to wrap it nest it and have the result as:

{
  "number_returned": 2, //returns result_list count
  "data": {
    "result_list": [
      {
        "lat": null,
        "lng": null,
        "locationId": null
      },
      {
        "lat": null,
        "lng": null,
        "locationId": null
      }
    ]
  }
}
yopipe
  • 1
  • 4

1 Answers1

1

I assume the simple idea (in pseudo code) is to do this:

new_json = {
    "number_returned": old_json.length,
    "data": {
        "result_list": old_json
    }
}

assuming that old_json is a string containing JSON you have, and new_json will be string with JSON you want to have; the javascript to do all you need will look like this:

var obj = JSON.parse(old_json);
var result_obj = {
    "number_returned": obj.length,
    "data": {
        "result_list": obj
    }
}
var new_json = JSON.stringify(result_obj);
webdev-dan
  • 1,339
  • 7
  • 10
  • 1
    This assumes the JSON is already parsed, and does not stringify the resulting object back to JSON. See: [What is the difference between JSON and Object Literal Notation?](/q/2904131/3982562) If this is what OP is looking for than OP also mixes up JavaScript objects and JSON. – 3limin4t0r Nov 27 '20 at 17:52
  • @3limin4t0r To be honest - I was aware that i mix object with json and i kind of did it on purpose. The question is not very clear what we talk about - so i answered with some kind of pseudo-code... but - well ...lets fix it – webdev-dan Nov 27 '20 at 20:11
  • @3limin4t0r hope You are satisfied now? :) don't forget to mark my answer as useful ;) – webdev-dan Nov 27 '20 at 20:29