skip to Main Content

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}"}

2

Answers


  1. Chosen as BEST ANSWER

    @Steffen I tried the following as the code you provided working but not working on my old javascript editor which cannot be updated. // To process header information //Declarations

    const hdr_str = '{RS0004036}:{1;2}:{0000003AB;0000003BC}_{RS0004036}:{0;3}:{0000003DE;0000003FG}_{RS0004036}:{3;3}:{0000003HI;0000003JK}';;
    var storeList = {};
    var partList = [];
    storeList.partList = partList;
    //Processing
    const partsDataEntries = hdr_str.split('_');
      for (var index = 0; index < partsDataEntries.length; index++) {
            const element = partsDataEntries[index].slice(1, -1);
        console.log('element0'+element);
            if (element != '') {
              const storepart = element.split(':');
              const storeId = storepart[0].slice(1, -1);
              const [qtyOnHand, qtyMinMax] = element.split(':')[1].split(';');
              console.log("element.split(':')[1]"+element.split(':')[1]);
          const qtyOH = qtyOnHand.substring(1,qtyOnHand.length);
           const   qtyMM = qtyMinMax.substring(0,qtyMinMax.length-1);
              const [partnum, partsub] = element.split(':')[2].split(';');
              const part = partnum.substring(1,partnum.length);
              storeList.storeId = storeId;
                          partList = {
                    "partnum": part,
                    "partsub": partsub,
                    "qtyOnHand": qtyOH,
                    "qtyMinMax": qtyMM 
                       }
              storeList.partList.push(partList);         
            }
          }
    console.log(JSON.stringify(storeList));

    //it is returning the following (repeating entries) in my javascript editor. How to fix above code. {"partList":[{"partnum":"0000003AB","partsub":"0000003BC","qtyOnHand":"0","qtyMinMax":"3"}{"partnum":"0000003AB","partsub":"0000003BC","qtyOnHand":"0","qtyMinMax":"3"},{"partnum":"0000003AB","partsub":"0000003BC","qtyOnHand":"0","qtyMinMax":"3"}],"storeId":"RS0004036"}


  2. I can’t help you debug your current code, because I don’t understand it. I’ve refactored it, to produce the result you were looking for.

    We use str.slice(1, -1)) to remove the {} around the data values. And then it’s as simple as splitting it apart and adding it to the array.

    const hdr_str = '{RS0004036}:{1;2}:{0000003AB;0000003BC}_{RS0004036}:{0;3}:{0000003DE;0000003FG}_{RS0004036}:{3;3}:{0000003HI;0000003JK}';
    const stores = [];
    
    const partsDataEntries = hdr_str.split('_');
    partsDataEntries.forEach(entry => {
      const partsData = entry.split(':').map(str => str.slice(1, -1));
      const storeId = partsData[0];
      const [qtyOnHand, qtyMinMax] = partsData[1].split(';');
      const [part, partSub] = partsData[2].split(';');
      
      if(!stores.find(store => store.store == storeId)) {
        stores.push({
          store: storeId,
          partList: []
        })
      }
      
      const store = stores.find(store => store.store == storeId);
      store.partList.push({
        part,
        partSub,
        qtyOnHand,
        qtyMinMax
      });
      
    });
    
    console.log(stores);

    Updated answer without .forEach() or .map()

    var hdr_str = '{RS0004036}:{1;2}:{0000003AB;0000003BC}_{RS0004036}:{0;3}:{0000003DE;0000003FG}_{RS0004036}:{3;3}:{0000003HI;0000003JK}';
    var stores = [];
    
    var partsDataEntries = hdr_str.split('_');
    for (var i = 0; i < partsDataEntries.length; i++) {
      var entry = partsDataEntries[i];
      var parts = entry.split(':');
      var partsData = [];
      
      for (var j = 0; j < parts.length; j++) {
        partsData.push(parts[j].slice(1, -1));
      }
      
      var storeId = partsData[0];
      var qtyData = partsData[1].split(';');
      var qtyOnHand = qtyData[0];
      var qtyMinMax = qtyData[1];
      var partData = partsData[2].split(';');
      var part = partData[0];
      var partSub = partData[1];
    
      var storeFound = false;
      for (var k = 0; k < stores.length; k++) {
        if (stores[k].store == storeId) {
          storeFound = true;
          break;
        }
      }
      
      if (!storeFound) {
        stores.push({
          store: storeId,
          partList: []
        });
      }
      
      var store;
      for (var l = 0; l < stores.length; l++) {
        if (stores[l].store == storeId) {
          store = stores[l];
          break;
        }
      }
      
      store.partList.push({
        part: part,
        partSub: partSub,
        qtyOnHand: qtyOnHand,
        qtyMinMax: qtyMinMax
      });
    }
    
    console.log(stores);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search