简体   繁体   中英

JS Convert a string into a json object

Need to turn a string into a json object.


Rules for json object / string:

  1. each iteration is incremented by one.
  2. split('|'), this is each row in the json object.
  3. split('^'), this is each field in the row.
  4. split('~'), this is each subfield in the field.
  5. needs to account for not having a subfield, field, or row
  6. 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"},
  }
];

Here you go, ready to be JSON.stringify 'd. Triple split followed by map gets you there:

 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"; var result = str.split('|').map(x => { return { row: x.split('^').map(y => { return { field: y.split('~').map(z => { return { subfield: z }; }) } }) } }); console.log(result); 

As you are not totally clear in your expected outcome, please let me know if anything is missing.

Edit : Added the same solution compatible with IE 11 below:

 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"; var result = str.split('|').map(function(x) { return { row: x.split('^').map(function(y) { return { field: y.split('~').map(function(z) { return { subfield: z }; }) } }) } }); console.log(result); 

Suggestion: you don't really want all those objects with key:value pairs where the key is simply the index. Your data will be easier to work with (and reflect the source structure better) if you simply have arrays of arrays (of arrays).

var str = 'FOO^one|BAR|TEST^one^two^a$b$c^three';
var obj = str.split('|')
        .map(s => s.split('^')
            .map(x => x.split('$')));

console.log(JSON.stringify(obj));

You can use reduce split and map.

 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){ return str.split(/[|]+/).reduce((op,inp,index)=>{ op[index]= inp .split(/[\\^]+/) .map((e,i)=>({[i]:e})) //change this if needed return op },{}) }; console.log(ParseDelimList(str)) 

I don't see ~ in provided input so i didn't included in answer.

you can just change the map incase you have ~ in input.

.map((e,i)=>({[i]:e.split(/[~]+/).map((e,i)=>({[i]:e})}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM