Announcement

Collapse
No announcement yet.

Can the miva_json_decode function handle nested JSON data?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Can the miva_json_decode function handle nested JSON data?

    Hey there!

    I need to get some specific data out of a JSON string. I've used the miva_json_decode function before and quite like it. But this time the JSON data I'm using is nested, and this seems to be a problem for the miva_json_decode function.

    Here's an example of what the nested data I'm working with looks like:

    {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,

    "batters":
    {
    "batter":
    [
    { "id": "1001", "type": "Regular" },
    { "id": "1002", "type": "Chocolate" },
    { "id": "1003", "type": "Blueberry" },
    { "id": "1004", "type": "Devil's Food" }
    ]
    },
    "topping":
    [
    { "id": "5001", "type": "None" },
    { "id": "5002", "type": "Glazed" },
    { "id": "5005", "type": "Sugar" },
    { "id": "5007", "type": "Powdered Sugar" },
    { "id": "5006", "type": "Chocolate with Sprinkles" },
    { "id": "5003", "type": "Chocolate" },
    { "id": "5004", "type": "Maple" }
    ]
    }


    The function does not return 1, aka it cannot seem to parse it. Any suggestions?

    EDIT: Sorry formatting on the json data didn't keep spacing. Copy paste that into a JSON beautifier if it helps you to be able to see the cascading.


    Thanks in advance!
    Jaime
    Last edited by jaime.v; 02-11-19, 01:18 PM.

    #2
    You can use miva_json_decode_last_error to get some more information on why the decoding is failing.
    David Carver
    Miva, Inc. | Software Developer

    Comment


      #3
      I just used your code above and it worked without issue... What you posted turned into:

      Code:
      :batters:batter[1]:id=1001
      :batters:batter[1]:type=Regular
      :batters:batter[2]:id=1002
      :batters:batter[2]:type=Chocolate
      :batters:batter[3]:id=1003
      :batters:batter[3]:type=Blueberry
      :batters:batter[4]:id=1004
      :batters:batter[4]:type=Devil%27s+Food
      :id=0001
      :name=Cake
      :ppu=0.55
      :topping[1]:id=5001
      :topping[1]:type=None
      :topping[2]:id=5002
      :topping[2]:type=Glazed
      :topping[3]:id=5005
      :topping[3]:type=Sugar
      :topping[4]:id=5007
      :topping[4]:type=Powdered+Sugar
      :topping[5]:id=5006
      :topping[5]:type=Chocolate+with+Sprinkles
      :topping[6]:id=5003
      :topping[6]:type=Chocolate
      :topping[7]:id=5004
      :topping[7]:type=Maple
      :type=donut
      Ryan Guisewite
      Lead UI Developer / Miva, Inc.
      www.miva.com

      Comment


        #4
        It is likely that the issue could have something to do with how you're receiving that data and passing it to miva_json_decode. Using the miva_json_decode_last_error() function like David suggested should point you in the right direction to fix it.
        Ryan Guisewite
        Lead UI Developer / Miva, Inc.
        www.miva.com

        Comment


          #5
          Thanks for the help! The miva_json_decode_last_error() function showed me I had a bracket issue with the raw json data. All fixed!

          Comment

          Working...
          X