Home > Software design >  Regex for add quotes around values without quotes (number and boolean)
Regex for add quotes around values without quotes (number and boolean)

Time:08-12

I receive a valid JSON file, but I would like to add " around the number values ​​and the boolean values ​​to send this JSON in a wix repeater which asks for " doubles around the number and boolean values

If someone could help me please

Exemple JSON received :

[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1}]

Exemple JSON needed:

[{"id":"1238890","category_id":"1","season_id":"14866","venue_id":"null","referee_id":"null","slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":"0","max":"2700","timestamp":"1660296540","extra":"540"}, "_id":"1"}]

I tried the code below to transform the json, without the desired result

J'ai essayé ce code là en transformant mon json en chaine string mais sans le résultat escompté..

const regex = /[^"\d,]?(\d )/g;
const str = [{"id":"1238890","category_id":"1","season_id":"14866","venue_id":"null","referee_id":"null","slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":"0","max":"2700","timestamp":"1660296540","extra":"540"}, "_id":"1"}]

const subst = `:`   `"$1"`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

Cordially

CodePudding user response:

Parse the json, re-stringify using a replacer function (see MDN). Below snippet also quotes null:

const json = JSON.parse(`[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1, "someBoolean":false}]`);
 
console.log(JSON.stringify(json, 
  (key, value) => 
    /number|boolean/.test(typeof value) || value === null ? `${value}` : value, 2));
.as-console-wrapper {
    max-height: 100% !important;
}

CodePudding user response:

I would rather just convert the object than use regex

const json = `[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1}]`

const data = JSON.parse(json)

const converted = convertToStrings(data)

console.log(converted)

function convertToStrings(obj) {
  if (['boolean', 'number'].includes(typeof obj)) {
    return obj.toString()
  }
  if (obj === null) {
    return 'null'
  }
  if (typeof obj === 'string') {
    return obj
  }
  if (Array.isArray(obj)) {
    const newArray = []
    for (const item of obj) {
      newArray.push(convertToStrings(item))
    }
    return newArray
  }
  if (typeof obj === 'object') {
    const newObj = {}
    for (const key in obj) {
      newObj[key] = convertToStrings(obj[key])
    }
    return newObj
  }
  throw new Error('Unsupported property type '   typeof obj)
}

  • Related