Need to turn a string into a json object.
Rules for json object / string:
- each iteration is incremented by one.
- split('|'), this is each row in the json object.
- split('^'), this is each field in the row.
- split('~'), this is each subfield in the field.
- needs to account for not having a subfield, field, or row
- Needs to run in Internet Explorer 11, pre ES6
String Example:
var str = "1863707152859270^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:25:52 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070|1863707222859280^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:27:02 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070"
Attempts:
var str = "1863707152859270^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:25:52 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070|1863707222859280^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:27:02 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070"
function ParseDelimList(str){
var result=[];
var rows = str.split('|');
var tmpString3 = [];
for(var i=0;i<rows.length;i++){
var fields = rows[i].split('^');
var tmpString2 = [];
for(var j=0;j<fields.length;j++){
var subfields = fields[j].split('~');
var tmpString1 = [];
for(var l=0;l<subfields.length;l++){
var tmp1 = "{"+l+":"+subfields[l]+"},"
tmpString1.push(tmp1);
};
var tmp2 = "{"+j+":"+tmpString1[j]+"},";
tmpString2.push(tmp2);
};
var tmp3 = "{"+i+":"+tmpString2[i]+"},";
tmpString3.push(tmp3);
};
return tmpString3;
};
console.log(ParseDelimList(str))
End result (something like this):
var json = [
"1":{
{"0":"1863707152859270"},
{"1":"Exercise to lose weight"},
{"2":"289169006"},
{"3":"Reduce 20 pounds"},
{"4":"Walk daily for one hour. Have a light dinner."},
{"5":"5/10/2013 12:00:00 AM"},
{"6":"1/21/2019 4:25:52 PM"},
{"7":"Y"},
{"8":"Frank the Tank"},
{"9":"1/22/2019 8:50:02 AM"},
{"10":"1/22/2019 8:50:02 AM"},
{"11":"Frank the Tank"},
{"12":"Abnormal LGSIL"},
{"13":"1848065703239670"},
{"14":"1863707006859070"},
},
"2":{
{"0":"1863707152859270"},
{"1":"Exercise to lose weight"},
{"2":"289169006"},
{"3":"Reduce 20 pounds"},
{"4":"Walk daily for one hour. Have a light dinner."},
{"5":"5/10/2013 12:00:00 AM"},
{"6":"1/21/2019 4:25:52 PM"},
{"7":"Y"},
{"8":"Frank the Tank"},
{"9":"1/22/2019 8:50:02 AM"},
{"10":"1/22/2019 8:50:02 AM"},
{"11":"Frank the Tank"},
{"12":"Abnormal LGSIL"},
{"13":"1848065703239670"},
{"14":"1863707006859070"},
}
];
JSON.stringify()to convert the whole thing to JSON.var tmp2 = "{"+j+":"+tmpString1[j]+"},";That's creating JSON by hand, and it's also invalid.