• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

JASON Data Size & Speed and Arrays VS Named Elements

Hi i am new to JASON arrays/objects but need to enhance old site with new Ajax & Jason. So I have a blank starting point and can use any sensible concept.

I am looking for ideas and also if you think that what I am going ot do is sensible.

Here is the problem:

In JASON I notice that if you want to create an array/object with named elements you need to format each and every record with names for each of the elements (I got this example from the web):

"variables":[{"children":null,"name":"asset_tag","order":1300,"question_text":"Asset Tag","type":6,"value":""},
"name":"Test one","order":-1,"question_text":"Testing","type":0,"value":null},
{"children":null,"name":"","order":200,"question_text":"Please specify an operating system","type":3,"value":"Windows XP"},
{"children":[{"children":null,"name":"create_item","order":1000,"question_text":"Create an Inventory Item?","type":7,"value":""}],
{"children":null,"name":"serial_number","order":1100,"question_text":"Serial Number","type":6,"value":""},
{"children":null,"name":"hard_drive","order":100,"question_text":"What size hard drive do you want?","type":3,"value":"60 GB"},
{"children":null,"name":"name","order":1200,"question_text":"Name of new inventory item","type":6,"value":""},

So you get your array of objects each objected with named elements.

The problem with that is for a few hundred records the names are repeated in each record, making the data size somewhat bloated.

Proposed Solution:

My Proposed solution is to create an object with a schema which names the fields and points to the element of each field.


var myRecords=

["Alen",37, "Happy"],
Element names are once only and not repeated, so requires a lot less space. You can still pragmatically access elements by name in this way:

        // display all names

      var message="";
        var nameElement = myRecords.schema["name"];

      for (var ct = 0; ct < myRecords.data.length; ct++){
            message = message +  myRecords.data[ct][nameElement]+ "<br>";

Does it make sense to use a schema object and data array combination?

Does anybody have any better ideas?

And finally is there a way of creating an array that is 2 dimensional so the data could be accessed is this form myRecords[row,column],  I have not worked out how to code this type of array using JASON?
  • 2
1 Solution
inthedarkAuthor Commented:
I guess I could further simplify the code in the schema, as the element is not really required.


var nameElement = myRecords.schema.index("name");

JSON (JavaScript Object Notaion) is just a representation of javascript data structures. For an Array, you would just have something like:


Open in new window

for and Object:

{item1:1, item2:2, item3:3}

Open in new window

and so on.

For a 2D Array, something like:


Open in new window

To convert a JSON string to an object, you can use the eval(); function, or there are libraries out there that do safe conversions (eval() does not specifically check for JSON syntax).

You can also declare an object programmatically and convert to JSON:

var myObj = new Object();
myObj.stuff = new Array();
var x = JSON.stringify(myObj);

x is {"stuff":["one","two","three"]}

Older broswer versions will need a library as only new browsers have the JSON object built in.

inthedarkAuthor Commented:

I went with arrays in the end with a combined Schema. I then have the beast of both worlds small data, and still able to access elements by using named element pointers, using indexOf().

function createJasonObject(){
      var tt={"schema":["name","age","job"],
      "data":[   ["Allen",37, "Programmer"],
            ["Nick",25,"CSS Coder"],

      var reps="";
      var nameElement = tt.schema.indexOf("job");
      for (var ct = 0; ct<tt.data.length; ct++){
            reps=reps + tt.data[ct][nameElement] + "<br>";
      return reps;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now