Reputation: 795
I have a JSON encoded object retrieved from a mysql table with the following format:
{immovable_property:
"{"immovable_subdivision":["a","b","c","d"],
"immovable_prop_details":["a","b ","c","d"],
"immovable_cost":["a","b","c","d"],
"immovable_present_value":["a","b","c","d"],
"immovable_owner":["a","b","c","d"],
"immovable_how_acquired":["a","b","c","d"],
"immovable_annual_income":["a","b","c","d"],
"immovable_remarks":["a","b","c","d"],
"add_subdivision":"e",
"add_prop_details":"e",
"add_cost":"e",
"add_present_value":"e",
"add_owner":"e",
"add_how_acquired":"e",
"add_annual_income":"e",
"add_remarks":"e "}"
}
I am unable to print the values of the keys and honestly I'm not sure whether it is proper JSON format. Any help is highly appreciated.
EDITED My code for retrieving the data using Laravel is as follows:
$data = Property::where("users_id","=",Input::get('user_id'))->select('immovable_property')->first();
return Response::json($data);
I printed the returned object using:
document.write(data.immovable_property);
which gives the output validated by JSONLint:
{
"immovable_subdivision": [
"a",
"b",
"c",
"d"
],
"immovable_prop_details": [
"a",
"b ",
"c",
"d"
],
"immovable_cost": [
"a",
"b",
"c",
"d"
],
"immovable_present_value": [
"a",
"b",
"c",
"d"
],
"immovable_owner": [
"a",
"b",
"c",
"d"
],
"immovable_how_acquired": [
"a",
"b",
"c",
"d"
],
"immovable_annual_income": [
"a",
"b",
"c",
"d"
],
"immovable_remarks": [
"a",
"b",
"c",
"d"
],
"add_subdivision": "e",
"add_prop_details": "e",
"add_cost": "e",
"add_present_value": "e",
"add_owner": "e",
"add_how_acquired": "e",
"add_annual_income": "e",
"add_remarks": "e "
}
Upvotes: 1
Views: 1679
Reputation: 1074266
Your edit completely changes the question.
Yes, that JSON is valid. You parse it via JSON.parse
:
var obj = JSON.parse(immovable_property);
...and then access the named properties on the result, e.g.:
console.log(obj.immovable_subdivision.length); // 4
You can get a list of all of the property names via Object.keys
, which returns an array.
Example:
// Your string
var immovable_property =
'{' +
' "immovable_subdivision": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_prop_details": [' +
' "a",' +
' "b ",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_cost": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_present_value": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_owner": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_how_acquired": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_annual_income": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_remarks": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "add_subdivision": "e",' +
' "add_prop_details": "e",' +
' "add_cost": "e",' +
' "add_present_value": "e",' +
' "add_owner": "e",' +
' "add_how_acquired": "e",' +
' "add_annual_income": "e",' +
' "add_remarks": "e "' +
'}';
// Parse it
var obj = JSON.parse(immovable_property);
// Access property via dot notation with a literal name
snippet.log('obj.immovable_subdivision.length = ' + obj.immovable_subdivision.length); // 4
// Or via brackets notation with a string namne
snippet.log('obj["immovable_subdivision"].length = ' + obj["immovable_subdivision"].length); // 4
// You can get all the keys (property names) as an
// array of strings from Object.keys, which lets you
// output them using bracketed notation
snippet.log("All properties:");
Object.keys(obj).forEach(function(key) {
snippet.log('obj["' + key + '"] = ' + obj[key]);
});
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Upvotes: 3