Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create 4 arrays from parsed JSON string

I have a parsed Json string that i would like to further split into 4 different arrays. I have tried using $.parseJson('[' + data + ']'); which returns all of the data into a single array. More Precisely i need it to look more like catData = [Object, Object, Object, Object] Thanks for your help.

my data coming in looks like this

  [{\"LkpMasterID\":491,\"LkpMasterCode\":\"CAT INSURANCE\",\"LkpMasterDescription\":\"25\",\"Attribute\":\"Cat Values\",\"Dependency\":null,\"LkpName\":\"CAT INSURANCE\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":52834,\"CreatedOn\":\"2015-09-15T15:25:46.273\",\"UpdatedBy\":null,\"UpdatedOn\":null,\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":1},{\"LkpMasterID\":492,\"LkpMasterCode\":\"CAT SUPPLEMENTS\",\"LkpMasterDescription\":\"50\",\"Attribute\":\"Cat Values\",\"Dependency\":null,\"LkpName\":\"CAT SUPPLEMENTS\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":52834,\"CreatedOn\":\"2015-09-15T15:28:36.2\",\"UpdatedBy\":56366,\"UpdatedOn\":\"2015-09-16T10:26:36.95\",\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":2},{\"LkpMasterID\":493,\"LkpMasterCode\":\"OTHER CATS\",\"LkpMasterDescription\":\"30\",\"Attribute\":\"Cat Values\",\"Dependency\":null,\"LkpName\":\"OTHER CATS\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":56366,\"CreatedOn\":\"2015-09-16T10:27:37.777\",\"UpdatedBy\":null,\"UpdatedOn\":null,\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":3},{\"LkpMasterID\":495,\"LkpMasterCode\":\"SHORT-TERM CATS\",\"LkpMasterDescription\":\"30\",\"Attribute\":\"CAT Values\",\"Dependency\":null,\"LkpName\":\"SHORT-TERM CATS\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":56366,\"CreatedOn\":\"2015-10-02T00:00:00\",\"UpdatedBy\":null,\"UpdatedOn\":null,\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":4}]"


$.getJson('LkpMasterTable', function (data) { var catData = JSON.Parse(data)) }   

var catData = [
 {"LkpMasterID":491,"LkpMasterCode":"CAT  INSURANCE","LkpMasterDescription":"25","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT INSURANCE","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:25:46.273","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":1},

 {"LkpMasterID":492,"LkpMasterCode":"CAT SUPPLEMENTS","LkpMasterDescription":"50","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT SUPPLEMENTS","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:28:36.2","UpdatedBy":56366,"UpdatedOn":"2015-09-16T10:26:36.95","IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":2},

 {"LkpMasterID":493,"LkpMasterCode":"OTHER CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"OTHER CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-09-16T10:27:37.777","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":3},

 {"LkpMasterID":495,"LkpMasterCode":"SHORT-TERM CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"SHORT-TERM CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-10-02T00:00:00","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":4}
  ]
like image 881
Leonardo Wildt Avatar asked Oct 19 '22 00:10

Leonardo Wildt


1 Answers

FIDDLE

What you can do is use jQuery's map function to "Translate all items in an array or object to new array of items."

var catData = [
 {"LkpMasterID":491,"LkpMasterCode":"CAT  INSURANCE","LkpMasterDescription":"25","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT INSURANCE","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:25:46.273","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":1},

 {"LkpMasterID":492,"LkpMasterCode":"CAT SUPPLEMENTS","LkpMasterDescription":"50","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT SUPPLEMENTS","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:28:36.2","UpdatedBy":56366,"UpdatedOn":"2015-09-16T10:26:36.95","IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":2},

 {"LkpMasterID":493,"LkpMasterCode":"OTHER CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"OTHER CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-09-16T10:27:37.777","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":3},

 {"LkpMasterID":495,"LkpMasterCode":"SHORT-TERM CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"SHORT-TERM CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-10-02T00:00:00","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":4}
  ]

var arr = $.map(catData, function(x) { return x; })

console.log(arr)

The results of using map look like 'catData = [Object, Object, Object, Object]'.

like image 160
Jonathan Kittell Avatar answered Oct 30 '22 14:10

Jonathan Kittell