Announcement

Collapse
No announcement yet.

How does the miva_json_decode function structure its output array?

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

    How does the miva_json_decode function structure its output array?

    I've got a small piece of JSON that I needed parsed. I want to retrieve only one specific field. I used the miva_json_decode(input, output) function to do the first part of the job.
    Here's tan example piece of JSON data:

    {
    "data": [{
    "id": 535819,
    "item_id": 114292,
    "warehouse_id": 1,
    "obtainable": 0,
    "created_at": "2017-07-28 16:29:42",
    "updated_at": "2017-07-28 16:29:42"
    }]
    }

    It seems to parse fine, as there are no errors received when I use miva_json_decode_last_error(). When I display the entire output, it shows all the contents just fine (though out of order) like so:

    2017-07-28 16:29:42,535819,114292,0,2017-07-28 16:29:42,1

    The issue arises when trying to access a specific element of this array. The docs say the function outputs as a structured array.But I can't find any documentation on how it structures the array whatsoever. I've just been trying an endless combination of indexes, and member names but I can't seem to access any of the elements.

    I tried miva_array_max and min to try and figure out how it's structured. But this returns -1 for both, an impossible index. So I'm confused as to how miva_json_decode REALLY outputs!

    Any info would be helpful.


    Thank You in Advance,
    Jaime


    #2
    <MvEval expr="{ output:data[1]item_id }"> should work for showing the item_id.
    Hope that helps!
    William Gilligan - Orange Marmalade, Inc.
    www.OrangeMarmaladeinc.com

    Comment


      #3
      The docs don't quite tell the whole story. the json_decode function can convert JSON into a single value, or an array with numbered elements, or an object with named members, or some combination of those. For your example, if you translate the JSON into a variable named l.result, then l.result will be an object with one member named l.result:data. l.result:data will be an array with one element. l.result:data[1] will be an object with members named l.result:data[1]:id, l.result:data[1]:item_id, l.result:data[1]:warehouse_id, etc.

      If you display an entire array or object with a single MvEVAL, the engine will format it as a comma-separated list of values, with all the element numbers or member names left out. If you want to write code that examines a variable to find out what its structure is, you can use the built-in functions, miva_array_elements() and miva_struct_members().

      Hope that clarifies things?
      Kent Multer
      Magic Metal Productions
      http://TheMagicM.com
      * Web developer/designer
      * E-commerce and Miva
      * Author, The Official Miva Web Scripting Book -- available on-line:
      http://www.amazon.com/exec/obidos/IS...icmetalproducA

      Comment


        #4
        Both of those replies help very much! Thank you!

        Comment

        Working...
        X