FutureWeb
1 year ago

using recursion and getting json keys which double as array names

Posted 1 year ago by FutureWeb

I have the below json to parse, this can go to any depth it contains an array called groups and within groups are sub groups which contain an id and a description the description doubles as an array name for a sub sub group which again contains an id and a description which in the instance below then contains and array of items id, groupingId, title, desc.

What I am trying to do is extract the id and description of the first 2 levels so group.id=2 group.description=Inclusive Services and sub_group.parent_id=2 sub_group.id=255-3 sub_group.description=Service Attributes and so on.

the problem I have found is I cannot seem to get the description of the group or sub group as it is an array.

{
  "groups": [
    {
      "id": "2",
      "Inclusive Services": [
        {
          "id": "255-3",
          "Service Attributes": [
            {
              "id": "13",
              "groupingId": "M3",
              "title": "BlackBerry Services",
              "desc": "Use the internet, email and BBM on your BlackBerry to share messages, pictures and more for free. Sync with your PC or Mac and take your favourite music, photos and videos wherever you go."
            }
          ]
        },
        {
          "id": "255-5",
          "Enhanced customer service": [
            {
              "id": "79",
              "groupingId": "M5",
              "title": "O2 Pay and Go Rewards",
              "desc": "As a new customer, when you join O2 Pay & Go and opt into O2 Rewards, you''ll earn 5% back on all top-ups every three months. So, if you top up £50, you can get £2.50 back at the end of the quarter. Even better, after you''ve been with us for six months, the % of rewards you earn will increase to 10%."
            }
          ]
        },
        {
          "id": "255-11",
          "Roaming services": [
            {
              "id": "73",
              "groupingId": "M11",
              "title": "O2 Travel",
              "suffix": "For the length of your contract",
              "desc": "O2 Travel is your perfect holiday companion.You can download TripAdvisor City Guides for reviews on things to do, the best places to eat and maps you can use offline. And if you are missing home, you can talk for up to an hour for a connection charge of just 50p and access all the data you need for just £1.99 per day."
            }
          ]
        },
        {
          "id": "255-4",
          "Entertainment freebies and discounts": [
            {
              "id": "64",
              "groupingId": "M4",
              "title": "O2 Priority",
              "suffix": "For the length of your contract",
              "desc": "Priority from O2 gives you access to exclusive offers and events including lunch for £1 every Monday. Grab some VIP treatment for you and your friends with access to gig tickets up to 48 hours before general release, exclusive entry to O2 bars and side step the queues with fast track lanes at O2 venues."
            },
            {
              "id": "65",
              "groupingId": "M4",
              "title": "O2 Tracks",
              "suffix": "For the length of your contract",
              "desc": "O2 Tracks gives you nonstop access to all the hottest music videos, top 40 playlists and backstage gossip. Try it for free with a four week free trial when you join O2 – no ads, no streaming, just great music."
            }
          ]
        },
        {
          "id": "255-23",
          "Free or discounted allowances": [
            {
              "id": "68",
              "groupingId": "M23",
              "title": "O2 TU Go",
              "desc": "TU Go is a free app that lets you use your O2 number on smartphones, tablets and laptops.It''s just like using your phone. Sign in to TU Go using a wifi connection to get your calls, texts and voicemail on up to 10 devices. The person you''re talking to doesn''t even need to have the app.Our latest version of TU Go now also lets you use the app over any mobile data (3G / 4G) connection – so you can now use TU Go for calls, texts and voicemails whenever you have mobile data available. If you want to use mobile data for TU Go calls, you can switch this on in Settings."
            },
            {
              "id": "69",
              "groupingId": "M23",
              "title": "O2 WiFi Hotspots",
              "desc": "Stay connected with O2 WiFi Hotspots available in thousands of locations nationwide including All Bar One, Debenhams, Café Rouge, Costa, House of Fraser, McDonalds and Subway."
            }
          ]
        },
        {
          "id": "255-25",
          "Free or discounted software": [
            {
              "id": "80",
              "groupingId": "M25",
              "title": "Evernote Premium for a year",
              "desc": "Experience Evernote Premium free for 1 year. With Evernote Premium you can capture your ideas, tasks, projects and more. You can share and edit your notes, access your work anywhere offline and get 1GB of free uploads each month."
            }
          ]
        }
      ]
    },
    {
      "id": "4",
      "Comes with (chargeable)": [
        {
          "id": "255-2",
          "Pay as you go Top-up": [
            {
              "id": "5",
              "groupingId": "M2",
              "title": "Pay as you go Top-up: £20",
              "suffix": "Topup",
              "cost_type": "Fixed",
              "cost": "20.00",
              "desc": "This product comes with a mandatory top-up charged at £20, meaning you will have £20 credit to use (or on your account)."
            }
          ]
        }
      ]
    }
  ]
}

Please sign in or create an account to participate in this conversation.