JSON Extension Functions

This website is no longer actively supported

Written by John DeVightJohn DeVight on 24 Jun 2011 15:06

Download the source code here.

Overview

I do a lot of client side coding in my ASP.NET applications. The data that I work with is in JSON format. Since a deal with arrays of JSON objects, I extended the JSON class to include additional functions to work with arrays of JSON objects.

To ensure that I had a JSON object to work with, I downloaded the json2.js file from the following site: https://github.com/douglascrockford/JSON-js

How to use the Extension Functions

JSON.findArrayElement()

Description:
Get the first occurrence of the JSON element in the array.

Syntax:
JSON.findArrayElement(objJsonArray, elementName, elementValue);

Return Value:
element found in the array or null if not found.

Examples:

var sampleJsonArray = [{FirstName: 'William', LastName: 'Adama', Role: 'Commander'},
                                {FirstName: 'Lee', LastName: 'Adama', Role: 'Pilot'},
                                {FirstName: 'Kara', LastName: 'Thrace', Role: 'Pilot'}];

// Get the first occurrence of the JSON element where the FirstName is Lee.
var leeadama = JSON.findArrayElement(sampleJsonArray, 'FirstName', 'Lee');

JSON.findAllArrayElements()

Description:
Get all occurrences of the JSON element in the array.

Syntax:
JSON.findAllArrayElements(objJsonArray, elementName, elementValue);

Return Value:
array of elements found in the array. The array will be empty if no elements are found.

Examples:

var sampleJsonArray = [{FirstName: 'William', LastName: 'Adama', Role: 'Commander'},
                                {FirstName: 'Lee', LastName: 'Adama', Role: 'Pilot'},
                                {FirstName: 'Kara', LastName: 'Thrace', Role: 'Pilot'}];

// Get the all occurrences of the JSON element where the Role is Pilot.
var leeadama = JSON.findAllArrayElements(sampleJsonArray, 'Role', 'Pilot');

JSON.indexOfArrayElement()

Description:
Get the index of the first occurence of the JSON element in the array.

Syntax:
JSON.indexOfArrayElement(objJsonArray, elementName, elementValue);

Return Value:
index of the element found in the array or null if not found.

Examples:

var sampleJsonArray = [{FirstName: 'William', LastName: 'Adama', Role: 'Commander'},
                                {FirstName: 'Lee', LastName: 'Adama', Role: 'Pilot'},
                                {FirstName: 'Kara', LastName: 'Thrace', Role: 'Pilot'}];

// Get the index of the first occurence of the JSON element where the 'Role' is 'Commander'.
var idx = JSON.indexOfArrayElement(sampleJsonArray, 'Role', 'Commander');

JSON.removeArrayElement()

Description:
Remove the first occurence of the JSON element in the array.

Syntax:
JSON.removeArrayElement(objJsonArray, elementName, elementValue);

Return Value:
None

Examples:

var sampleJsonArray = [{FirstName: 'William', LastName: 'Adama', Role: 'Commander'},
                                {FirstName: 'Lee', LastName: 'Adama', Role: 'Pilot'},
                                {FirstName: 'Kara', LastName: 'Thrace', Role: 'Pilot'}];

// Remove the first occurence of the JSON element where the 'Role' is 'Commander'.
JSON.removeArrayElement(sampleJsonArray, 'Role', 'Commander');

JSON.removeArrayElementByIndex()

Description:
Remove the first occurence of the JSON element in the array using the zero based index passed in.

Syntax:
JSON.removeArrayElementByIndex(objJsonArray, idx);

Return Value:
None

Examples:

var sampleJsonArray = [{FirstName: 'William', LastName: 'Adama', Role: 'Commander'},
                                {FirstName: 'Lee', LastName: 'Adama', Role: 'Pilot'},
                                {FirstName: 'Kara', LastName: 'Thrace', Role: 'Pilot'}];

// Remove the second JSON element from the array.
JSON.removeArrayElement(sampleJsonArray, 1);

JSON.replaceArrayElement()

Description:
Replace a JSON element in the array with a new JSON element.

Syntax:
JSON.replaceArrayElement(objJsonArray, elementName, elementValue, newJsonElement);

Return Value:
index of the element replaced in the array or null if not found.

Examples:

var sampleJsonArray = [{FirstName: 'William', LastName: 'Adama', Role: 'Commander'},
                                {FirstName: 'Lee', LastName: 'Adama', Role: 'Pilot'},
                                {FirstName: 'Kara', LastName: 'Thrace', Role: 'Pilot'}];

// Replace Lee Adama with a new role of President.
JSON.removeArrayElement(sampleJsonArray, 'FirstName', 'Lee', 
    { FirstName: 'Lee', LastName: 'Adama', Role: 'President' });

JSON.sortArrayElements()

Description:
Sort the elements in the JSON array.

Syntax:
JSON.sortArrayElements(objJsonArray, elementName, reverse, [optional: primer]);

Return Value:
None

Examples:

var sampleJsonArray = [{FirstName: 'William', LastName: 'Adama', Role: 'Commander', Age: 50},
                                {FirstName: 'Lee', LastName: 'Adama', Role: 'Pilot', Age: 29},
                                {FirstName: 'Kara', LastName: 'Thrace', Role: 'Pilot', Age: 27}];

// Sort ascending by LastName.
var sortedJsonArray = JSON.sortArrayElements(sampleJsonArray, 'LastName', false);

// Sort descending by Age.
// The "primer" that is passed in converts the value to an integer before performing the comparison.
var sortedJsonArray = JSON.sortArrayElements(sampleJsonArray, 'Age', true, 
    function(a) { return parseInt(a); });

References

Support ASP.NET Wiki

If you like this page, click on the "Share on" links in the wikidot toolbar at the top of the page to share it with your friends.

Comments

Add a New Comment

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License