0

I have a JSON Array like this.

{"records":[{"ColorId":"3","ColorName":"Red","GenderId":"3","GenderName":"Female","SizeId":"1","SizeName":"N\/A"},{"ColorId":"2","ColorName":"Green","GenderId":"2","GenderName":"Male","SizeId":"1","SizeName":"N\/A"},{"ColorId":"3","ColorName":"Red","GenderId":"2","GenderName":"Male","SizeId":"1","SizeName":"N\/A"}]}

I need to separate these variables into separate objects with distinct

Color : ColorId,ColorName

Gender : GenderId,GenderName

Size: SizeId,SizeName

5
  • which json library you are using? Commented Aug 1, 2016 at 12:50
  • i have just used jquery Commented Aug 1, 2016 at 12:52
  • please tell me, what is a json library, @KalpenPatel? Commented Aug 1, 2016 at 12:56
  • If you want to do using jquery than we can do it using JSON.parse("your json string"). and than you have write code to get data from it. once you parse json data using JSON.parse it will return json object. and using it you can find length of data using object.length in jquery. and from it you can retrieve data from your json Commented Aug 1, 2016 at 13:00
  • Then you talk about a javascript library, not a json library, @KalpenPatel. Commented Aug 1, 2016 at 14:16

1 Answer 1

1

It is just a simple loop of the recods and creating your data in an own structure. There is even no need to use of any jQuery functions.

for( var i = 0, l = data.records.length; i < l; i++ ) {
    color.push({ColorId: data.records[i].ColorId, ColorName: data.records[i].ColorName});
    gender.push({GenderId: data.records[i].GenderId, GenderName: data.records[i].GenderName});
    size.push({SizeId: data.records[i].SizeId, SizeName: data.records[i].SizeName});
}

Wokring example.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!. It Works , But i need those to be distinct too ,, how can i get that to work
Assuming the IDs are unique, the easiest way would be to switch from array to an object and just use the id as property, so it would be distinct: jsfiddle.net/ufjk5j80/1 @HudhaifaYoosuf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.