0

I downloaded some JSON, as shown below. In the developer console it looks like an object containing nested objects which contain nested objects ...

I would like to add a new, empty "campaign" into the data (at the front). How do I do that?

Insert

var blankCampaignData = {'title': '', 'description': '', 'path_to_logo': '', 'start_time': date, 'end_time' : date, 'paused': false};

into

{
    "campaigns": {
        "1": {
            "campaign_id": "1",
            "title": "Nike Air 2015 campaign",
            "description": null,
            "path_to_logo": null,
            "start_time": "09/11/2015 22:42:08",
            "end_time": "09/03/2016 22:42:08",
            "paused": "0",
            "destinations": {
                "1": {
                    "destination_id": "1",
                    "campaign_id": "1",
                    "url": "www.nike.com/nike_air",
                    "description": "Nike air destination",
                    "connections": {
                        "3": {
                            "connection_id": "3",
                            "destination_id": "1",
                            "tag_id": "0",
                            "country": "Scotland",
                            "county": "Yorkshire",
                            "town": "East Ham",
                            "post_code": "SE1 1AA",
                            "custom": "bus stop",
                            "description": "Connection number 3"
                        }
                    }
                },
                "2": {
                    "destination_id": "2",
                    "campaign_id": "1",
                    "url": "www.nike.com/nike_air/sub_campaign",
                    "description": "Nike air - free laces promotion destination",
                    "connections": {
                        "2": {
                            "connection_id": "2",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Engerland",
                            "county": "Devon",
                            "town": "East Ham",
                            "post_code": "SE1 1AA",
                            "custom": "bus stop",
                            "description": "Connection number 2"
                        },
                        "4": {
                            "connection_id": "4",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Engerland",
                            "county": "Yorkshire",
                            "town": "Felixswtowe",
                            "post_code": "RB3 9YR",
                            "custom": "police staticon",
                            "description": "Connection number 4"
                        },
                        "6": {
                            "connection_id": "6",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Scotland",
                            "county": "Essex",
                            "town": "York",
                            "post_code": "JD8 4LF",
                            "custom": "somewhere else",
                            "description": "Connection number 6"
                        },
                        "9": {
                            "connection_id": "9",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Scotland",
                            "county": "Cork",
                            "town": "York",
                            "post_code": "JD8 4LF",
                            "custom": "in the ladies' loo",
                            "description": "Connection number 9"
                        }
                    }
                }
            }
        },
        "2": {
            "campaign_id": "2",
            "title": "Nike football boots campaign",
            "description": null,
            "path_to_logo": null,
            "start_time": "09/12/2015 22:42:08",
            "end_time": "09/01/2016 22:42:08",
            "paused": "0",
            "destinations": {
                "3": {
                    "destination_id": "3",
                    "campaign_id": "2",
                    "url": "www.nike.com/nike_football_boots/",
                    "description": "Nike footie boots destination",
                    "connections": {}
                },
                "4": {
                    "destination_id": "4",
                    "campaign_id": "2",
                    "url": "www.nike.com/nike_football_boots/sub_campaign",
                    "description": "Buy left boot, get right boot free destination",
                    "connections": {}
                }
            }
        },
        "3": {
            "campaign_id": "3",
            "title": "Nike general promotion campaign",
            "description": null,
            "path_to_logo": null,
            "start_time": "09/12/2013 22:42:08",
            "end_time": "09/08/2016 22:42:08",
            "paused": "0",
            "destinations": {
                "5": {
                    "destination_id": "5",
                    "campaign_id": "3",
                    "url": "www.nike.com/general_promotion",
                    "description": "Nike general promotion destination",
                    "connections": {}
                },
                "6": {
                    "destination_id": "6",
                    "campaign_id": "3",
                    "url": "www.nike.com/general_promotion/discount_coupon",
                    "description": "20% off coupon destination",
                    "connections": {}
                }
            }
        }
    }
}
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

1 Answers1

1

Work out what the next campaign id should be:

var nextId = +Object.keys(obj.campaigns).sort().pop() + 1;

Add the empty campaign to the campaigns object. Obviously you'll need to define date beforehand.

obj.campaigns[nextId] = {
  'title': '',
  'description': '',
  'path_to_logo': '',
  'start_time': date,
  'end_time' : date,
  'paused': false
}
Andy
  • 61,948
  • 13
  • 68
  • 95
  • Thanks, I will try that. Btw, I would prefer to add it at the front – Mawg says reinstate Monica Dec 09 '15 at 22:58
  • 1
    You can't. Objects aren't sorted. If `campaigns` was an array then you could [`unshift`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) the new object as the first element. – Andy Dec 09 '15 at 22:59
  • To add it at the front, cold I take the new object and add the existing one to the end of that? – Mawg says reinstate Monica Dec 10 '15 at 00:09
  • 1
    "In front" and "behind" make no sense because objects __are not ordered__. The only way for it to make sense would be to be to move the object with key 1 to a new key and replace it with the new object. – Andy Dec 10 '15 at 00:13
  • A good point (+1), but I will `ng-repeat` over the data and want the new one to appear first on the screen. I will either do as you sat and overwrite the first key (but that would mean shuffling every entry down, to insert at front) or append the existing object to the new, empty one. – Mawg says reinstate Monica Dec 10 '15 at 07:51
  • 1
    You'll either have to adjust your PHP to give you an array of objects then, or do what I suggested and move the object with id=1 to a new object and then assign the new object to key 1. Sounds like changing the PHP would be better for you in the long run. – Andy Dec 10 '15 at 07:54
  • 1
    I wondered if I could do that. Your comments spurred me to search and I found http://stackoverflow.com/questions/22408311/return-an-array-of-objects `while ($row = $stmt->fetch(PDO::FETCH_OBJ))` Thanks!! Now I only have to figure out how to get PHP to nest the tree structure (each campaign has multiple destinations, each of which have multiple connections), so I use 3 nested loops, each with a `SELECT` statement – Mawg says reinstate Monica Dec 10 '15 at 08:07
  • I can't figure it out, so I posted a new question at http://stackoverflow.com/questions/34196818/return-an-array-of-array-of-arrays-of-objects Future readers of this question may want to look there (as might @Andy ;-) – Mawg says reinstate Monica Dec 10 '15 at 08:21