1
[{
        "name":"John"
        "age":19,
        "hobby":"Basketball;play computer"
    },
    {
        "name":"Anderson"
        "age":19,
        "hobby":"Tennis"
    }
    ]

John have 2 hobbies, it suppose to be in array but I have no control of the source of the api. How can I make the json to be below format?

[{
        "name":"John"
        "age":19,
        "hobby":"Basketball"
    },{
        "name":"John"
        "age":19,
        "hobby":"play computer"
    },
    {
        "name":"Anderson"
        "age":19,
        "hobby":"Tennis"
    }
    ]

I'm new to jquery so here's code I've tried :

var hobbies = "";
$.each(json, function(){
hobbies = this.hobby.split(',');
});
9
  • @Satpal I stuck at split. Can u helps? Commented Sep 23, 2015 at 7:17
  • 5
    Why duplicate most of john's personal data ? Why not just something like "hobby":[ "Basketball", "play computer" ] ? Commented Sep 23, 2015 at 7:17
  • 1
    this is invalid without the , after first element right? Commented Sep 23, 2015 at 7:19
  • @Pekka, that must be typo while posting question Commented Sep 23, 2015 at 7:19
  • 1
    @SivaNatalie OK, add the code Commented Sep 23, 2015 at 7:22

3 Answers 3

3
var data = [{
    "name": "John",
        "age": 19,
        "hobby": "Basketball;play computer"
}, {
    "name": "Anderson",
        "age": 19,
        "hobby": "Tennis"
}]

$.each(data, function (index, value) {
    if (value.hobby.split(';').length > 1) {
        var dataArray = value.hobby.split(';');
        value.hobby = dataArray[0];
        dataArray.shift();
        $.each(dataArray, function (innerIndex, innerValue) {
            data.push({
                "name": value.name,
                "age": value.age,
                "hobby": innerValue
            });
        });
    }
});

console.log(data);

Fiddle Demo

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

5 Comments

awsome than you so much, didn't thought of loop within a loop!
@SivaNatalie glad to help :)
one problem, how to trim off the spaces of hobbies? I tried dataArray = dataArray.trim(); but I got it's not a function error.
just curious why is it value.hobby = dataArray[0] ? what's that? why u assign to value.hobby and never use back?
@SivaNatalie the orignal array object which contains multiple values, is changed to a single value with this line.
0
            var arr = [{
            "name":"John",
            "age":19,
            "hobby":"Basketball;play computer"
            },
            {
            "name":"Anderson",
            "age":19,
            "hobby":"Tennis"
            }
            ];
            $.each(arr, function(i,j){
            var temp = j.hobby;
            var hobby_arr = temp.split(';');
            j.hobby = hobby_arr;
            });

Try this. However there is an error in your provided json. There should be a ',' after the 'name' value

1 Comment

No it convert you response with the proper format, with the hobby array. Without repeating the whole.
0

Here is a fiddle of your working thing ( assuming that ";" is the separator )

http://jsfiddle.net/swaprks/vcpq8dtr/

var json = [{
        "name":"John",
        "age":19,
        "hobby":"Basketball;play computer"
    },
    {
        "name":"Anderson",
        "age":19,
        "hobby":"Tennis"
    }
    ];

$(function(){
    for ( var i = 0; i < json.length; i++ ) {
        var obj = json[i];
        if ( obj["hobby"].indexOf(";") != -1 ){
            var hobbyArr = obj["hobby"].split(";");
            for ( var j = 0; j < hobbyArr.length; j++ ){
                var newObj = {};
                if ( j == 0 ){

                    json[i]["hobby"] = hobbyArr[j];
                } else {

                    newObj = {
                            "name": obj["name"],
                            "age": obj["age"],
                            "hobby": hobbyArr[j]
                        }
                    json.push(newObj);

                }
            }
        }
    }
    console.log(json)
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.