Deepak Desai Deepak Desai - 9 months ago 46
JSON Question

how to change the below format to json format using javascript

I want to convert this data format:

"color;blue:36 ,red:27"

to the following JSON format:

"Record": [
"type": "color",
"data": "blue",
"total": "36"
"type": "color",
"data": "red",
"total": "27"

Answer Source

It's hard to understand the exact problem with the given input, but that should suffice:

    var input = "color;blue:36 ,red:27";

    // Reassignment is bad, but will shorten the example
    input = input.split(";");

    var attributeName = input[0];

    // No length validations, also bad
    var attributes = input[1].split(",");

    var results = {"Record": {
        a = a.split(":");
        return {"type":attributeName,
            "total": a[1].trim()}