User Guide Cancel

IsJSON

 

Description

Evaluates whether a string is in valid JSON (JavaScript Object Notation) data interchange format.

Returns

True if the parameter is a valid JSON value.False if the parameter is not a valid JSON data representation.

Category

Syntax

IsJSON(var)
IsJSON(var)
IsJSON(var)

See also

History

ColdFusion 8: Added function

Parameters

Parameter

Description

var

A string or variable that represents one.

Example

This example checks whether the data feed that is generated by the example for the SerializeJSON function contains valid JSON data.The feed is in the form of a JavaScript function call where the parameter is a JSON string that contains the feed data. The example does the following operations:

  1. Uses a cfhttp tag to get the feed (in the cfhttp.fileContent variable).
  2. Strips the function call wrapper from the text.
  3. Uses the IsJSON function to check whether the result of the previous step is a valid JSON format string.
  4. Displays a message that indicates whether the text is valid JSON data, followed by the feed text string.To run this example, put this file and the example for the SerializeJSONfunction in an appropriate location under your ColdFusion web root, replace the URL with the correct URL for the serialization example, and run this page.

 

<cfscript>
myObj= {name: "John", age: 31, city: "New York"};
myJSON = IsJSON(myObj);
writeoutput(myJSON);
</cfscript>
<cfscript> myObj= {name: "John", age: 31, city: "New York"}; myJSON = IsJSON(myObj); writeoutput(myJSON); </cfscript>
<cfscript>
 myObj= {name: "John", age: 31, city: "New York"};
 myJSON = IsJSON(myObj);
 writeoutput(myJSON);
</cfscript>

Output

NO

For a more complex example that then converts the JSON data, see DeserializeJSON.

Get help faster and easier

New user?