Friday, January 8, 2016

Filter out only the necessary values from s JSON String

Have you ever needed to filter out a JSON and get only the values that you want. Try with following JavaScript application. Definitely you will need many customization.
 

<html>
    <head>
        <script language="javascript">
            var jsObject = null;


            //Function to convert the json string to javascript object
            function extractFromJson(jsonText){
                jsObject = JSON.parse(jsonText);          
            }

            //Function to filter out values under a specific property
            function filterjsObject(key){
                if(key != ""){ //if object has multiple properties
                    var object = jsObject[key];
                }else{ //if object is single
                    var object = jsObject;              
                }
                var combinedValues = "";
                for(var key in object){
                    combinedValues = combinedValues + object[key] + '<br>';          
                }
                console.log(combinedValues);
                document.getElementById('plainJsonValues').innerHTML = combinedValues;
            }
          
            //Function to get JSON key list
            function getJsonKeys(jsonString){
                var select = document.getElementById("jsonProperties");
                var jsonString = JSON.parse(document.getElementById('jsonString').value);
                var jsonKeys = Object.keys(jsonString);
                console.log(jsonString);
                var properties = "";              

                for(var i = 0; i < jsonKeys.length; i++) {
                    properties = properties + jsonKeys[i] + '<br>';
                }
              
                document.getElementById('jsonProperties').innerHTML = properties;
            }
          
            //Function execution point
            function execute(){
                extractFromJson(document.getElementById('jsonString').value);
                filterjsObject(document.getElementById('jsonKey').value);
                getJsonKeys(document.getElementById('jsonString').value);          
            }

        </script>
    </head>


    <body>
        <p>JSON String : </p>
        <textarea id="jsonString" cols="80" rows="10"></textarea> <br>
        <p>JSON property (Optional) : </p>
        <input type="text" id="jsonKey"><br><br>
        <input type="button" onClick="execute()" value ="Process"><br>
        <p>JSON properties : </p>
        <p id="jsonProperties"></p>
        <p>Values for selected properties : </p>
        <p id="plainJsonValues"></p>
      
    </body>

</html>

Tuesday, January 5, 2016

Extract only the values from a select element on a web page hosted in internet

Do you need to extract only the values from a html select element which is on a web site hosted in the internet.

  • Then just copy and paste following html code and save it with any_name.html and open it with a web browser
  • Go to the site and get the relevant html code set for the select box.(You can extract it using a tool like web developer tools available in the browser)
  • Copy and paste above code set on the text area of our generated web page.  
  • Then click process button on the page
  • It will filter out only the required values.


<html>
    <head>
        <script language="javascript">
            function getModels(){
                var selectContent = document.getElementById("selectContent").value;
                var indexToStart = selectContent.indexOf(">") + 1;
                var targetString = selectContent.substring(indexToStart);
                var optionSet = targetString.split("</option>");
                var optionArrLength = optionSet.length;
                var modelSet = [];

                for(var counter = 0 ; counter < optionArrLength; counter++){   
                    modelSet[counter] = (optionSet[counter].split(">"))[1];
                }
               
                var resultLength = modelSet.length;
                var currentDivContent = "";
                for(var counter = 0; counter < resultLength; counter ++){
                    currentDivContent = currentDivContent + modelSet[counter] + "<br>";
                   
                }
                document.getElementById("resultText").innerHTML = currentDivContent;   
                console.log(currentDivContent);
            }
        </script>
    </head>

    <body>
       
        <form>
            <textarea id = "selectContent" rows=20 cols=100></textarea>
            <input type="button" value="Process" OnClick="getModels()">
        </form>
       
        <div>
            <p id = "resultText"></p>
        </div>
    </body>
</html>





Monday, January 4, 2016

USSD shortcode to get your Own mobile phone number in Sri Lanka

  • Go to normal dial pad from any mobile phone device (Feature or Smart phone)
  • Type and dial #132#
  • You will get an USSD application response from the operator including your basic  information.
  • Above short code support for any service provider in Sri Lanka. (Tested with Mobitel and Dialog)