-1

I have the following data:

{
"action":"Profile Updates",
"candidate_id":"1234",
"docs_verified":"ABC",
"skills":"PROF1_EN, blah, test"
}

I need the data in JSON format but I need the skills section as an array... how can I convert this JSON to one which has the skills section as an array?

I can separate just the skills but I'm unsure how to return this:

{
    "action": "Profile Updates",
    "candidate_id": "1234",
    "docs_verified": "ABC",
    "skills": ["PROF1_EN, blah, test"]
}
Tim
  • 563
  • 2
  • 6
  • 20
  • Please [edit] your question to show what research you've done and any attempts you've made to solve the issue yourself. Note that there exists the question [How can I convert a comma-separated string to an array?](https://stackoverflow.com/q/2858121/215552) – Heretic Monkey Jun 30 '21 at 20:38
  • Do you mean `"skills": ["PROF1_EN", "blah", "test"]` ? – Kinglish Jun 30 '21 at 20:53
  • Does this answer your question? [Convert string with commas to array](https://stackoverflow.com/questions/13272406/convert-string-with-commas-to-array) – Kinglish Jun 30 '21 at 20:53

2 Answers2

1
const obj = {
  "action":"Profile Updates",
  "candidate_id":"1234",
  "docs_verified":"ABC",
  "skills":"PROF1_EN, blah, test"
  }

const output = JSON.stringify({...obj, skills: [obj.skills]});

console.log(output)

We don't know the context, but take it from here and massage it to your liking

user776686
  • 7,933
  • 14
  • 71
  • 124
  • imho - The question is an obvious dupe. Best to close as such rather than give dupe answers – Kinglish Jun 30 '21 at 20:55
  • @Kinglish This is up to moderator to check if it is a dupe, and if it is - provide a link to the original. Not my responsibility – user776686 Jun 30 '21 at 20:57
  • that returns me this "{\"action\":\"Profile Updates\",\"candidate_id\":\"1234\",\"docs_verified\":\"ABC\",\"skills\":[\"PROF1_EN, blah, test\"]}". – Tim Jun 30 '21 at 20:59
  • @Tim Oh well, I don't know how you are testing, but at least you know how to approach data massaging you need. – user776686 Jun 30 '21 at 21:04
  • I think you meant `output={...obj, skills: [obj.skills]};`. OP doesn't need a string, they need a modified object ... also console.log can show objects just fine without stringify - and it's preferrable it view them without stringify b/c it shows structure – Kinglish Jun 30 '21 at 21:10
  • Then feel free to downvote it. I am not going an extra mile if the OP doesn't. – user776686 Jun 30 '21 at 21:13
0

You can use the split method to convert a string to an array based on a function

var obj = {
  "action":"Profile Updates",
  "candidate_id":"1234",
  "docs_verified":"ABC",
  "skills":"PROF1_EN, blah, test"
};
obj["skills"] = obj["skills"].split(" ");
  • this doesn't give the OP the correct answer, even if the OP corrects their expected result (which not likely their actual expected result) - this would make skills:`["PROF1_EN,", "blah,","test"]` – Kinglish Jun 30 '21 at 20:52