1

I have a response string like this:

|||COVID HPI^^^FEVER^^^Low Grade
|||COVID HPI^^^FEVER^^^Continuous
|||COVID HPI^^^RHINORRHEA/SNEEZING^^^No
|||GENERAL EXAM^^^CL^^^Conscious
|||GENERAL EXAM^^^ORIENTED^^^Yes

And i want to convert it into something like this:

{
  "COVID HPI": [
    {
      "FEVER": "Low Grade, Continuous",
      "RHINORRHEA/SNEEZING": "Yes"
    }
  ],
  "GENERAL EXAM": [
    {
      "CL": "Conscious",
      "ORIENTED": "Yes"
    }
  ]
}

I am not able to go beyond this without an ugly code:

const na = [];
    fetch('http://localhost/test/json/newdata.json')
    .then(response => {
       let vv = response.json();
       return vv;
    })
    .then(function(data){
    console.log(data[0]['tRxVal'])
        let nav = data[0]['tRxVal'].slice(3).split('|||');
        let nbv = nav.map(function(item,i){
            return item.split('^^^');
        });
        //console.log(nbv);
        nbv.forEach(function(v,i){
            if(!na.includes(v[0])){
                na.push(v[0]);
            }
        })
        console.log(na)

    })
    .catch(err => {
        // handle errors
    });

Can someone please help me with this Thanks

2 Answers 2

3

You should try to use the split function. You could do a first split on the string for "|||", after that you could split all the resulting strings for "^^^", so that you can divide keys and values.

Something like that (take it as pseudocode)

let strings = data.split("|||");
for (string in strings){
  let objectKeyValue = string.split("^^^");
  let object = objectKeyValue[0];
  let key = objectKeyValue[1];
  let value = objectKeyValue[2];
}

from here, compose your object and you're good to go.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

Sign up to request clarification or add additional context in comments.

Comments

2

Split by ||| to get each row, and then split each row by ^^^. Then, you can think of each "row" like this: <topLevelCategory>^^^<subCategory>^^^<subCategoryAnswer>. Use this mental model of each row to build your object around.

const string = `|||COVID HPI^^^FEVER^^^Low Grade
|||COVID HPI^^^FEVER^^^Continuous
|||COVID HPI^^^RHINORRHEA/SNEEZING^^^No
|||GENERAL EXAM^^^CL^^^Conscious
|||GENERAL EXAM^^^ORIENTED^^^Yes`;

function stringToObj(str) {
  const branches = str.split('|||');
  const obj = {};
  branches.forEach((branch) => {
    const leaves = branch.split('^^^');
    const [top, sub, answer] = leaves;
    if (top) {
      if (!obj[top]) obj[top] = {};
      const trimmedAnswer = answer.trim();
      const curr = obj[top][sub];
      if (curr) {
        obj[top][sub] = `${curr}, ${trimmedAnswer}`;
      } else {
        obj[top][sub] = answer.trim();
      }
    }
  });

  return obj;
} 

console.log(stringToObj(string));

2 Comments

Thanks Lucas. This is nice and its getting close. But its its missing out on 'sub' having 2 answers. Like in the above FEVER has 2 answers.
Awesome. Thank you LMulvey and Lucas. This is terrific and clears up lots of issues

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.