PeterPlanlos PeterPlanlos - 3 months ago 13
JSON Question

Javascript Trim left and right from an JSOn response

i search a easy way to trim left something from my json response and trim right something from my json output.

as example
my json how it is:

{"something":[{"id":"1","name":"Test1"},{"id":"2","name":"Test2"},{"id":"3","name":"Test3"}]}


how i want it to be!:

[{"id":"1","name":"Test1"},{"id":"2","name":"Test2"},{"id":"3","name":"Test3"}]


as you can see i want TrimLeft all before
[
and TrimRight alls behing
]


and this is where i have my json response in: res

function responseHandler(res) {

return res;
}


Thank you very much!

Answer

The easiest way to go would probably to just save this json to a variable and access the content of something similar like this:

var jsonOutput = {"something":[{"id":"1","name":"Test1"},{"id":"2","name":"Test2"},{"id":"3","name":"Test3"}]};
var something = jsonOutput.something;
console.log(something);

The output should be the expected json.

Edit 1

Referring to your edit I add another piece of code to come up to your solution.

function responseHandler(response) { 
    var result = response.something;
    return result ; 
}

This should give you the expected response.