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":""},
{"children":[{"children":null,"name":"ste1","order":-1,"question_text":"Test","type":6,"value":"test"}],
"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":""}],
"name":null,"order":-1,"question_text":"Options","type":0,"value":null},
{"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":""},
{"children":null,"name":"company","order":1400,"question_text":"Manufacturer","type":8,"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=

{"schema":
   {"name":0,"age":1,"status":2},
"data":[
["Alen",37, "Happy"],
["Nick",62,"Overworked"],
["Bill",45,"Stressed"]]
};
      
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?
LVL 17
inthedarkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

inthedarkAuthor Commented:
I guess I could further simplify the code in the schema, as the element is not really required.

{"schema":
   {["name","age":"status"]},


var nameElement = myRecords.schema.index("name");
0
smeghammerCommented:
Hi,

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

[1,2,3,4,5]

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:

[["a1","a2"],["b1","b2"],["c1","c2"]]

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();
myObj.stuff.push("one");
myObj.stuff.push("two");
myObj.stuff.push("three");
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.

Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
inthedarkAuthor Commented:
Thanks.

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"],
            ["Sally",29,"Designer"]]
            };
      

      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;
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.