Working with JSON in Javascript


JSON is a very popular format in programming and support by most of modern languages. In Javascript, it natively supports JSON.

Parse JSON text to Javascript object

We have a variable assigned to this JSON text.

 var jsonin = '{"employees":[
  {"firstName":"Config9", "lastName":"COM"},
  {"firstName":"Inneka", "lastName":"COM"},
  {"firstName":"oraerr", "lastName":"COM"}

Now use JSON.parse() with this text

var jsonobj = JSON.parse(jsonin);

Now we have an object, we can access elements in this object as normal.


Dump JSON object to text output

we can use JSON.stringify() method to dump JSON object as follow:

var jsontxt = JSON.stringify(jsonobj, undefined, 4);

Happy coding!


angularjs ng-disabled doesn’t add disabled to button


angularjs ng-disabled doesn’t add disabled to button

I have an input button that I want to set to disabled if a user selects a certain value from a select box:
Select menu:

Input Button:

Now when I am viewing the source, when I do select option 2, the input button element changes from value ng-disabled=”false” to ng-disabled=”true” but the disabled attribute is not applied to the button.
What am I doing wrong here?


Solution 1:

Use this

<input type="submit" class="btn btn-info pull-right btn-lg btn-block" ng-disabled="associate.JobStatus == 2" />

Solution 2:

Angular Doc ngDisabled

Curly braces means the notation {{ }} to bind expressions to elements is built-in Angular markup.


<input type="submit" ng-disabled="associate.JobStatus == 2" />

Instead of:

<input type="submit" ng-disabled="{{associate.JobStatus == 2}}" />

same as for ng-show / ng-hide / ng-if, you can use the model
expression itself instead of {{}}

For Example:-


<div ng-show="someObject.showProperty">

Instead of:

<div ng-show="{{someObject.showProperty}}">