Thanks Tim; that's great.

As a side note, it's interesting that I frequently get html responses to
error pages from apis that would otherwise return json. Like many of us,
they assume everything will work.

On Thu, Sep 1, 2016 at 11:03 AM, Timothy Penner <[email protected]> wrote:

> Hi Lee,
>
> I think what you are looking for is this:
> http://kb.4d.com/assetid=77555
>
> =======================
>
>
> Tech Tip: How to check if TEXT is an OBJECT / JSON
> PRODUCT: 4D | VERSION: 15.2 | PLATFORM: Mac & Win
> Published On: June 23, 2016
>
> Here is a utility method that can be used to check if some TEXT represents
> a valid OBJECT / JSON:
>
>
> // UTIL_IS_TEXT_AN_OBJECT
> // $1 := TEXT of JSON to check
> // $0 := BOOLEAN representing validitity
> If (Count parameters=1)
>    C_BOOLEAN($0)
>    C_TEXT($method;$result;$1)
>    $method:=Method called on error
>    ON ERR CALL(Current method name)
>    $result:=JSON Stringify(JSON Parse($1))
>    ON ERR CALL($method)
>    If ($result="")
>       $0:=False
>    Else
>       $0:=True
>    End if
> Else
>     // on err call
>     // do nothing
> End if
>
>
> If this method is saved as UTIL_IS_TEXT_AN_OBJECT it could be used like
> this:
>
> C_TEXT($text)
> $text:="{\"OK\": true}"
> If (UTIL_IS_TEXT_AN_OBJECT ($text))
>    C_OBJECT($ob)
>    $ob:=JSON Parse($text;Is object)
> Else
>    ALERT("$text is not an object")
> End if
>
>
> This is useful for checking arbitrary data before parsing it as an object
> because an error is generated if you attempt to parse invalid text as a
> object.
>
> ---
>
> -Tim PENNER
>
>
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to