I have the following string on a JavaScript variable:
my_Variable = "{"Domini":[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]}"
I need to remove the root at the top of string Domini
so the resultant string looks like this:
my_Variable = [{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]
I've tried to parse JSON to an object using JSON.parse()
function and it works, but then I don't know how to retrieve the data and handle it to generate a new JSON without root.
Should I be using some kind of loop?
I have the following string on a JavaScript variable:
my_Variable = "{"Domini":[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]}"
I need to remove the root at the top of string Domini
so the resultant string looks like this:
my_Variable = [{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]
I've tried to parse JSON to an object using JSON.parse()
function and it works, but then I don't know how to retrieve the data and handle it to generate a new JSON without root.
Should I be using some kind of loop?
Share Improve this question edited Dec 24, 2012 at 11:31 aLearner 1,04715 silver badges30 bronze badges asked Dec 24, 2012 at 11:25 Calypo GunnCalypo Gunn 1771 gold badge2 silver badges9 bronze badges 3- Why do you want to remove the root? Most of the times once you convert this string to JSON object anyhow you wont refer to this root. – Shekhar Commented Dec 24, 2012 at 11:26
-
Is the "root" always going to be named
Domini
? – Alexander Commented Dec 24, 2012 at 11:44 - Thank you very much to everybody! Actualy it was realy easy! – Calypo Gunn Commented Dec 24, 2012 at 11:47
4 Answers
Reset to default 6It's simple:
my_Variable = my_Variable.Domini;
After JSON parse.
So code will be like:
my_Variable = "{"Domini":[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","nom_domini":"Parc Natural del Montseny"},{"cod_domini":"5","nom_domini":"Sant Pere de Vilamajor"},{"cod_domini":"6","nom_domini":"Santa Maria i Mosqueroles"}]}";
my_Variable = JSON.parse( my_Variable );
my_Variable = my_Variable.Domini;
console.log( my_Variable ); // [{"cod_domini":"1","nom_domini": ...
var obj = JSON.parse(json);
obj = [Object.keys(obj)[0]];
var newJson = JSON.stringify(obj);
or, eschewing ES5 features like Object.keys:
var obj = JSON.parse(json);
for (var key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
obj = obj[key];
break;
}
var newJson = JSON.stringify(obj);
Considering you want it back to a string after removing the key.
my_Variable = JSON.stringify(JSON.parse(my_Variable).Domini);
Breakdown
JSON.parse(my_Variable)
Parses the string into a JSON object.
JSON.parse(my_Variable).Domini
Returns the value of the property Domini
.
JSON.stringify(JSON.parse(my_Variable).Domini)
Encodes it back into a string.
If you want your variable passed, do the following (you mentioned you had already done this, but I added it for pleteness)
var parsedJson = JSON.parse(my_Variable);
when you have parsed it, just do the following:
var JsonWithoutDominiAsRoot = parsedJson.Domini;
You can "." your way through different attributes of your new json object. Domini just being one of them.