Skip to content Skip to sidebar Skip to footer

How To Convert This Non-json String To Array Of Objects?

I’m getting this String from Firebase: '[{type:'salad', qt:0, price:0.25},{type:'cheese', qt:0, price:0.45},{type:'bacon', qt:0, price:0.75},{type:'meat', qt:0, price:4.00},]' I

Solution 1:

Although fixing the saved data is the best bet, this will work but its very "hacky".

Simply replace all of the keys with strings wrapped in double quotes and replace the single quotes with double quotes then use JSON.parse

str = "[{type:'salad', qt:0, price:0.25},{type:'cheese', qt:0, price:0.45},{type:'bacon', qt:0, price:0.75},{type:'meat', qt:0, price:4.00},]"

str = str.replace(/'/g, '"');
str = str.replace(/type/g, '"type"');
str = str.replace(/qt/g, '"qt"');
str = str.replace(/price/g, '"price"');

str = str.replace(",]","]")

arry = JSON.parse(str)

console.log(arry)

Solution 2:

Use regex:

const fireStr = "[{type:'salad', qt:0, price:0.25},{type:'cheese', qt:0, price:0.45},{type:'bacon', qt:0, price:0.75},{type:'meat', qt:0, price:4.00},]";

conststringCorrection = str => {
  str = str.replace(/,(?=(\]|\})$)/gmui, '');
  str = str.replace(/'/gmui, '');
  
  [...newSet(str.match(/[a-z]+/gmui))].forEach(el => {
    const reg = newRegExp(`${el}`, 'g', 'i');

    const str2 = `"${el}"`;

    str = str.replace(reg, str2);
  });


  return str;
}

const str = stringCorrection(fireStr);

const myArr = JSON.parse(str);

console.log(myArr);

Result:

[
  { type:'salad', qt:0, price:0.25 },
  { type:'cheese', qt:0, price:0.45 },
  { type:'bacon', qt:0, price:0.75 },
  { type:'meat', qt:0, price:4 }
]

Solution 3:

Based on comments and since whole thing goes south and since no one has come up with the answer as yet. here is the final solution. Keeping in mind string it self is not valid json this is where the problem rests.

var abc = "[{type:'salad', qt:0, price:0.25},{type:'cheese', qt:0, price:0.45},{type:'bacon', qt:0, price:0.75},{type:'meat', qt:0, price:4.00},]".replace(/\[|\]/gi, "").replace(/\},\{/gi, '}|{').split('|');

    //remove all the chunks and prepare for pre-processing//a function that takes the whole pre-processing string and converts into  valid jsonfunctionmakeitright(a){


     var jsonStrig = '[{';
              var items = a.replace(/\{|\}|'/gi, "").split(',');
              for (var i = 0; i < items.length; i++) {
                  var current = items[i].split(':');
                  jsonStrig += '"' + current[0] + '":"' + current[1] + '",';
              }
              jsonStrig = jsonStrig.substr(0, jsonStrig.length - 1);
              jsonStrig += '}]';
    returnJSON.parse(jsonStrig)[0];

    }

    //runs everything ina loop to give you teh array as desired.

    final = [];
    for (var i = 0; i < abc.length; i++){
    final.push(makeitright(abc[i]));
    }
    console.log(final[1].type);

This based on current question and string as stated in example string. If it had been valid json to begin with I would have stood with my previous answer. Hope this helps.

Post a Comment for "How To Convert This Non-json String To Array Of Objects?"