View Raw SPL
/*****************************************************************************
*                                                                            *
*   READJSON.SPL Copyright (C) 2020 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns a JSON object from a file or URL                    *
*                                                                            *
*   Revisions:   20 Aug 2020  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#include 


#if @HELP_READJSON

    READJSON

    Purpose: Returns a JSON object from a file or URL.

    Syntax:  READJSON("url", "header")

              "url"    - A string, the file or URL string.

              "header" - Optional. A string, the HTML header. Defaults to
                         "Content-Type: application/json"

    Returns: An object, the JSON object for the URL.

    Example:
             readjson(gethome() + "data\example.json")

             Displays an object from a JSON file that contains information
             on a set of superheros.

    Example:
             readjson("https://ipinfo.io/json")

             Displays an object that contains information on the current
             IP address.

    Example:
             readjson("http://dummy.restapiexample.com/api/v1/employees")

             Displays an object where the DATA property contains 24
             mocked employee records.

    Example:
             dspobj = readjson("http://dummy.restapiexample.com/api/v1/employees")

             Same as above except the returned JSON object is assigned to the
             variable DSPOBJ.

    Remarks:
             READJSON returns a JSON object for the input file or URL.
             The initial returned JSON string is converted to an object.

             If no output is provided, the object is displayed in a pop-up
             dialog box.

    See Also:
             Geturl
             Objjson
             Viewjson
             Webreadhtml
             Webreadimage
#endif


/* read json file or URL and return object */
readjson(fname = "", header = "Content-Type: application/json")
{
        local jstr, jobj;

        if (not(isstring(fname)) || strlen(fname) == 0)
        {
                error(sprintf(_rdterr_iur, __FUNC__));
        }

        if (fileexists(fname))
        {
                jstr = strfile(fname, 0, 1);
                jobj = objjson(jstr);
        }
        else
        {
                if (isvalidurl(fname))
                {
                        jobj = webreadjson(fname, header);
                }
                else
                {
                        error(sprintf("'%s' does not exist", fname));
                }
        }

        return(jobj);
}