Deepak Desai Deepak Desai - 1 year ago 83
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()}


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download