I have the following string coming in from users and need to turn it into JSON.
2r,5g|3b,2r,4y|3g
I would like to convert it to JSON with JavaScript. Here is what I would like the JSON to look like:
rounds = {
"round" : {
"interval" : {
"time" : "2",
"color" : "r"
},
"interval" : {
"time" : "5",
"color" : "g"
}
},
"round" : {
"interval" : {
"time" : "3",
"color" : "b"
},
"interval" : {
"time" : "2",
"color" : "r"
},
"interval" : {
"time" : "4",
"color" : "y"
}
},
"round" : {
"interval" : {
"time" : "3",
"color" : "g"
}
}
}
I’m not really sure where to start here so any help is appreciated!
You can use this code:
But I have to say, that JSON written by you is not valid because its’ fields must have unique names. I’ve used names like
round0,round1… andinterval0and so on.