Here is the input that should be converted to JSON format as given below.
'{RS0004036}:{1;2}:{0000003AB;0000003BC}_{RS0004036}:{0;3}:{0000003DE;0000003FG}_{RS0004036}:{3;3}:{0000003HI;0000003JK}'
Target JSON should be like this. The code should read the above input, will create a JSON return string as follows. The returning JSON will return store which will be only one value of the index 0 of the input string and will not repeat as partList.
"storeList": [
{
"store": "RS0004036",
"partList": [
{
"part": "0000003AB",
"partSub": "0000003BC",
"qtyOnHand": "0",
"qtyMinMax": "3"
},
{
"part": "0000003DE",
"partSub": "0000003FG",
"qtyOnHand": "3",
"qtyMinMax": "3"
},
{
"part": "0000003HI",
"partSub": "0000003JK",
"qtyOnHand": "1",
"qtyMinMax": "2"
}
]
}
]
I tried the following code:
String.prototype.replaceAll = function replaceAll(search, replace) {
return this.split(search).join(replace);
}
var storeList = {};
var partList = [];
storeList.partList = partList;
var hdr_str = '{RS0004036}:{1;2}:{0000003AB;0000003BC}_{RS0004036}:{0;3}:{0000003DE;0000003FG}_{RS0004036}:{3;3}:{0000003HI;0000003JK}';
var sites_ar = [];
var str_array = hdr_str.split('_');
var str_us = [];
var qty_ar = [];
var hdr_parts = [];
var part_semi = [];
var part_detail = [];
function populate_part(str_in) {
for (var i = 0; i < str_array.length; i++) {
if (str_array[i] !== '"') {
var str_colon = str_array[i];
if (str_colon !== '"') {
if (str_colon !== undefined) {
const part_detail = str_colon.split(':');
storeList.store = part_detail[0]
partList = {
"part": part_detail[0],
"qtyOnHand": part_detail[1],
"qtyMinMax": part_detail[2]
}
storeList.partList.push(partList);
}
}
}
}
}
pattern = ';';
replacement = ',';
hdr_str = hdr_str.replaceAll(pattern, replacement);
pattern = ':';
replacement = ',';
hdr_str = hdr_str.replaceAll(pattern, replacement);
hdr_str = '[' + hdr_str + ']';
for (var i = 0; i < hdr_str.length; i++) {
if (str_array[i] !== '"') {
populate_part(str_array[i]);
}
}
retun(JSON.stringify(storeList));
But it is returning duplicates, not sure where is the issue.
{"partList":[{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{0;3}","qtyMinMax":"{0000003DE;0000003FG}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"},{"part":"{RS0004036}","qtyOnHand":"{1;2}","qtyMinMax":"{0000003AB;0000003BC}"},{"part":"{RS0004036}","qtyOnHand":"{3;3}","qtyMinMax":"{0000003HI;0000003JK}"}],"store":"{RS0004036}"}