-1

I'll like to split the JSON object below to its respective keys and value using regular expression and wrap the values in a html p tag.

{
"completeOrder": [
    {
        "optionDescription": "Choose your size?",
        "optionTotal": 3,
        "dataMax": 0,
        "name": [
            "wheat",
            " dough",
            " rice"
        ],
        "price": [
            "34",
            "23",
            "45"
        ],
        "require": false,
        "dataSingleproduct": false,
        "innerOptions": [],
        "extraOptions": [
         ...
         ]
    }
   ]
}

The returned string i want would look like this:

{
 completeOrder: [{
optionDescription: 
<p>Choose your size</p>
optionTotal: 
<p>3</p>
dataMax: 
<p>0</p>
name: [
<p>Wheat</p>
...
]
...
]
}

This JSON object is saved in a file, received via node.js's "fs.read" feature. I receive the object in str variable, presently this is the code i have presently, its giving me close to what i want but it lacks the form i need.

let val= str.split(/[\r\n|\r|\n|{|}|:|,|\[|\]]/gi);
for (let i = 0; i < val.length; i++) {
    // if(sentences[i] !== ""){
        console.log(val[i])
    // }
}

My code strips the of the characters ("{", "[", "]", "}", ":") but i dont what them stripped. Any help would be appreciated

TREX_SON
  • 45
  • 7
  • A downgrade, why?, still no answers hmm. ok – TREX_SON Nov 22 '19 at 21:33
  • I think people on here are tired of people trying to abuse REXEPs. Anyway, why don't you just use any number of json parsers (possibly built in at this point) and get in into a real data structure, and then process it? – gahooa Nov 22 '19 at 23:54
  • Lol, that's funny never thought there was a thing as an abuse to REGEX, so plesse elaborate on how you mean **"json parsers (possibly built in at this point) and get in into a real data structure, and then process it?"**, this teritory is still fairly new to me. – TREX_SON Nov 23 '19 at 00:05
  • https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – gahooa Nov 25 '19 at 20:08

1 Answers1

0

You can recursively iterate over your JSON object and just put a paragraph tag around your value if its a literal.

function f (obj)
{
 for (let i in obj)
 {
  if (typeof obj[i] == "object")
  {
   f (obj[i]);
  }
  else
  {
   obj[i] = '<p>' + obj[i] + '</p>';
  }
 }
}

my_json = {
"completeOrder": [
    {
        "optionDescription": "Choose your size?",
        "optionTotal": 3,
        "dataMax": 0,
        "name": [
            "wheat",
            " dough",
            " rice"
        ],
        "price": [
            "34",
            "23",
            "45"
        ],
        "require": false,
        "dataSingleproduct": false,
        "innerOptions": [],
        "extraOptions": [
         ]
    }
   ]
}

f (my_json);
console.log ( JSON.stringify (my_json).replace (/\,/g, "\n" ) );
flappix
  • 2,038
  • 17
  • 28
  • Thank you much, did exactly what i wanted, i totally overlooked the possibility of using javascript to do this. – TREX_SON Nov 23 '19 at 00:26
  • Its taken me over an hour now, i believed, the rest would be easy,m how do i wrap tags around the Keys and probably the curly braces and square brackets, any idea. – TREX_SON Nov 23 '19 at 01:26
  • That's a bit more complicated. But if you just want to print a json object in a pretty-looking well formated HTML ouput, simply use ```document.body.innerHTML = '
    ' + JSON.stringify (json_obj, null, 2) + '
    ';``` No need to add any tags add all.
    – flappix Nov 26 '19 at 09:45
  • thanks i eventually made some complicated changes with replace and it worked perfectly thanks to your answer. – TREX_SON Nov 26 '19 at 23:15