bluewonder
bluewonder

Reputation: 43

Convert text string into JSON format - Javascript

I have one text string as following :

workingtable;AB8C;book_id;7541;

I would like to convert them into JSON format like : {"workingtable":"AB8C","book_id":"7541"}

Is there any JSON function so that I can convert the raw text string to JSON format like that in Javascript?

Thanks

Upvotes: 0

Views: 2704

Answers (1)

L.B
L.B

Reputation: 116108

 var s = "workingtable;AB8C;book_id;7541;";
 var parts = s.split(';');
 var jobj = {};
 for(i=0;i<parts.length;i+=2)
 {
    jobj[parts[i]]=parts[i+1];
 }
 alert(JSON.stringify(jobj));

OUTPUT:

{"workingtable":"AB8C","book_id":"7541"} 

Upvotes: 2

Related Questions