How to Use the JSON Decode Function in Mivascript

Miva Merchant Modules and Development
JSON is a great way to store and move text data. Decoding the JSON data into a usable array is also easy using Mivascript.

How to Use the JSON Decode Function in Mivascript

The Miva scripting language comes with a very nice JSON decode function. Here is how to use it.


JSON is a great way to store and move text data. Decoding the JSON data into a usable array is also easy using Mivascript.

by Scot Ranney • August 16, 2023

Miva Merchant Storemorph, Miva Scripting 101


The miva_json_decode(...) function in Mivascript is very easy to use. Better yet, it's wonderfully useful.

The miva json encode function is not built into Mivascript, however there is a json_encode(...) function in the Miva Merchant API that let's you use it in your modules or in template Store Morph Technology (SMT) code. We'll take a quick look at both of these and how they can be used in Miva Merchant templates.

miva_json_decode(...)

Here is a block of basic json set up in SMT code. Using MvCAPTURE is the easiest way to put the JSON into a variable because there is no need to deal with any special formatting.

<mvt:capture variable="l.settings:scientists">
{
"scientists": [
    {
        "name":"Albert Einstein",
        "ice_cream":"Vanilla",
        "idea":"Theory of Relativity"
    },
    {
        "name":"Ernest Rutherford",
        "ice_cream":"Pistacio",
        "idea":"Everything Edison Took Credit For"
    },
     {
        "name":"Stephen Hawkins",
        "ice_cream":"none",
        "idea":"Hawking radiation"
    }
]
}
</mvt:capture>

This JSON block is now stored in l.settings:scientists - note that in a module you would use the MvCAPTURE tag.

Hint: Using l.settings style variables in template coding (SMT) makes them super easy to display. &mvt:variable; is faster and cleaner looking than using the  <mvt:eval expr="l.some_variable" />

Make an array out of l.settings:scientists by using the miva_json_decode(...) function and display the data in a loop:

<mvt:assign name="l.json_convert_ok" value="miva_json_decode(l.settings:scientists,l.settings:json_array)" />

<mvt:if expr="l.json_convert_ok NE 1"> <p>JSON is invalid.</p> <mvt:else> <mvt:foreach iterator="scientist" array="json_array:scientists"> <p> <b>Name:</b> &mvt:scientist:name; <b>Favorite Ice Cream:</b> &mvt:scientist:ice_cream; <b>Popular idea:</b> &mvt:scientist:idea; </p> </mvt:foreach> </mvt:if>

The miva_json_decode(...) function returns a 1 if the JSON is decoded successfully. It's generally good to check for things like this so the code above includes an mvt:if conditional to make sure the data is OK.

The output of the code above is:

Name: Albert Einstein
Favorite Ice Cream: Vanilla
Idea: Theory of Relativity

Name: Ernest Rutherford
Favorite Ice Cream: Pistacio
Idea: Everything Edison Took Credit For

Name: Stephen Hawkins
Favorite Ice Cream: none
Idea: Hawking radiation


overall rating:
my rating: log in to rate

json mivascript stormorph tutorial

The blog posts on Scot's Scripts are made using by Scot's Blogger, a full featured Wordpress replacement Miva Merchant blogging module.