[Solved] How do I get the specfic json data name from this string [DB/Jscript question]

Ok say I have got these two JSON arrays, which are Entity custom data.

var entity = JSON.parse(qrydata2[1]);

 var test = "[{"Name":"Year","Value":"11"},{"Name":"Name","Value":"Jacob "},{"Name":"Email","Value":"[email protected]"},{"Name":"CName","Value":"Jacob "},{"Name":"Points","Value":"1"}"; ]
 var test = "[{"Name":"Year","Value":"12"},{"Name":"Email","Value":"[email protected]"},{"Name":"Points","Value":"24"},{"Name":"CName","Value":"Daniel "}";]

At the moment I’m using this code to parse the JSON

var entity = JSON.parse(test);
Then this code to get the certain data out of the JSON array.

var data;
data["Student Year"] = entity[0].Value;
data["Student Name"] = entity[1].Value;
data["Student Email"] = entity[2].Value;

However the two custom entity data have certain fields in different positions so this above code only works for some. How do I get it by the name so I dont have to worry about what postiton the field is in the JSON array.

Since the name of the subarray is a valu and not a name you would need to do a for loop.
I’ve, if it were single sub array where “year”:“2016” rather than “name”:“year”.“value”:"“2016” you could have used index of to get the index for the array name.

So in short a for loop then use the letter from the for in the index brackets of entity[ ]

So something like this (don’t know the for loop code by heart so will need changing…

for(i , i < 3 , i ++) {
var loopIndexName = entity[i].Name;
var loopIndexValue = entity[i].Value;
if(loopIndexName == ‘Year’) {
xxxxxxxxx
Xxxxxxx
}
}

Thanks for that :slight_smile: Ive got it working now

for(i =0; i < entity.length; i ++) {
            var name = entity[i].Name;
            var value = entity[i].Value;
            if(name == 'Year') {
                data["Student Year"] = entity[i].Value;
            }

            if(name == 'Email') {
                data["Student Email"] = entity[i].Value;
            }

            if(name == 'CName') {
                data["Student Name"] = entity[i].Value;
            }

            if(name == 'Points') {
                data["Points"] = entity[i].Value;
            }

        }

Ive not seen this before and am intrigued,
What are you achiving by using [quote=“the133448, post:3, topic:11090”]
data[“Student Name”]
[/quote]

rather than
data.name?

Pretty much so that I can add the entiy from a search box or so I tohught I have to put the entity name as an id number which is what would be on a barcode. CName is the person’s first and full name