i want to create jSon format from this string value:
hello|world|this|is|javascript || my|name|is|mahdi
in created jSon array from that i can not wrap this string to separator as 2 array by split with ||. my code is work fine but i can not create some array in json after split string with ||.
result my code is:
{
"FILEDS": [
{
"template_id": "123",
"fields_id": "456"
},
{
"item": "hello"
},
{
"item": "world"
},
{
"item": "this"
},
{
"item": "is"
},
{
"item": "javascript "
},
{
"item": " my"
},
{
"item": "name"
},
{
"item": "is"
},
{
"item": "mahdi"
}
]
}
but i want to have this result like with below json format:
{
"FILEDS": [
{
"template_id": "123",
"fields_id": "456"
},
[
{
"item": "hello"
},
{
"item": "world"
},
{
"item": "this"
},
{
"item": "is"
},
{
"item": "javascript "
}
],
[
{
"item": " my"
},
{
"item": "name"
},
{
"item": "is"
},
{
"item": "mahdi"
}
]
]
}
My code is below code and how to create this array in each for for some data to create and wrap to []?
<script type="text/javascript" language="JavaScript">
var data = "hello|world|this|is|javascript || my|name|is|mahdi";
var templates = {
FILEDS: []
};
templates.FILEDS.push({
"template_id": "123",
"fields_id": "456",
});
var split_data = data.split("||");
for (var i = 0; i < split_data.length; i++) {
var fields = split_data[i].split("|");
for (var j = 0; j < fields.length; j++) {
templates.FILEDS.push({
"item": fields[j],
});
}
}
console.log(JSON.stringify(templates));
</script>