Saturday, 8 May 2021

Jq: Processing curl command output

Just like how you are piping json data from echo command, from a file, you can even extract the curl command output using jq tool.

 

Example 1: Get name of github project.

$curl https://api.github.com/repos/harikrishna553/springboot | jq '.name'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5601  100  5601    0     0   6182      0 --:--:-- --:--:-- --:--:--  6175
"springboot"

 

Example 2: get url of github project.

$curl https://api.github.com/repos/harikrishna553/springboot | jq '.html_url'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5601  100  5601    0     0   6350      0 --:--:-- --:--:-- --:--:--  6343
"https://github.com/harikrishna553/springboot"

 

Example 3: Get name, url, size and full name of the github project.

$curl https://api.github.com/repos/harikrishna553/springboot | jq '.name, .html_url, .size, .full_name'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5601  100  5601    0     0   6237      0 --:--:-- --:--:-- --:--:--  6230
"springboot"
"https://github.com/harikrishna553/springboot"
520
"harikrishna553/springboot"

 

 

 

 

 



Previous                                                    Next                                                    Home

jq: []: Navigate array

[] operator is used to navigate arrays.

 

empPretty.json 

{
  "org": [{
    "name": "Honeywell",
    "yrsOfExperience": 2.2
  }, {
    "name": "IBM",
    "yrsOfExperience": 1.8
  }],
  "firstName": "Krishna",
  "lastName": "Ram",
  "address": {
    "city": "Bangalore",
    "country": "India"
  }
}

 

For example,

.org[0].name gives "Honeywell",

.org[1]. yrsOfExperience gives 1.8.

$cat empPretty.json | jq '.org[0]'
{
  "name": "Honeywell",
  "yrsOfExperience": 2.2
}
$
$cat empPretty.json | jq '.org[0].name'
"Honeywell"
$
$cat empPretty.json | jq '.org[1]'
{
  "name": "IBM",
  "yrsOfExperience": 1.8
}
$
$cat empPretty.json | jq '.org[1].yrsOfExperience'
1.8

 

How to get all the organization names that employee worked?

Simply skip the array index parameter.

 

Example

.org[].name

 

Above snippet return all the organization names.

$cat empPretty.json | jq '.org[].name'
"Honeywell"
"IBM"

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Php: Add methods to the class definition

Functions defined inside a class are called methods. Methods are used to perform some action on instance/object properties.

 

Example

class Employee{
    var $id;
    var $first_name;
    var $last_name;
    var $country='India';

    function about_me(){
        echo "\nid : $this->id\n";
        echo "first_name : $this->first_name\n";
        echo "last_name : $this->last_name\n";
        echo "country : $this->country\n";
    }
}

 

Above snippet defines an Employee class and about_me function. As you observe the definition of about_me function, I am using $this(it is a special variable to access current instance properties inside the class) to access current object properties.

 

How to call the method using object?

Syntax

$object_name->method_name(arguments);

 

Example

$emp1 -> about_me();

 

Find the below working application.

 

instance_methods_demo.php

#!/usr/bin/php

<?php

   class Employee{
        var $id;
        var $first_name;
        var $last_name;
        var $country='India';

        function about_me(){
            echo "id : $this->id\n";
            echo "first_name : $this->first_name\n";
            echo "last_name : $this->last_name\n";
            echo "country : $this->country\n";
        }
   }

   $emp1 = new Employee;
   $emp1 -> id = 1;
   $emp1 -> first_name = 'Krishna';
   $emp1 -> last_name = 'Gurram';
   
   $emp1 -> about_me();
?>

Output

$./instance_methods_demo.php 

id : 1
first_name : Krishna
last_name : Gurram
country : India

 

 

 


 

 

 

 

 

Previous                                                    Next                                                    Home

Php: Add properties to class definition

 

Every object in the world has some properties associated with it. For example, an employee has properties id, first_name, last_name etc.,

 

How to define a property?

Use var keyword followed by $variable_name inside class definition.

 

Can I set a default value to a property?

Yes, you can do.

 

Example

class Employee{

    var $id;

    var $first_name;

    var $last_name;

    var $country='India';

}

 

Above snippet defines an Employee with following properties.

a.   $id, $first_name and $last_name

b.   $country with default value as 'India'.

 

How to define values to instance properties?

Step 1: Define instance/object from a class.

$emp1 = new Employee;

  

Step 2: Use the syntax ‘$object_name -> property_name’ to set value to instance properties.

 

$emp1 -> id = 1;

$emp1 -> first_name = 'Krishna';

$emp1 -> last_name = 'Gurram';

 

Can I define multiple instances/object from same class definition?

Yes, you can do.

 

Find the below working application.

 

instance_properties_demo.php

#!/usr/bin/php

<?php

   function print_employee($emp){
       echo "\nid : $emp->id\n";
       echo "first_name : $emp->first_name\n";
       echo "last_name : $emp->last_name\n";
       echo "country : $emp->country\n";
   }

   class Employee{
        var $id;
        var $first_name;
        var $last_name;
        var $country='India';
   }

   $emp1 = new Employee;
   $emp1 -> id = 1;
   $emp1 -> first_name = 'Krishna';
   $emp1 -> last_name = 'Gurram';

   $emp2 = new Employee;
   $emp2 -> id = 2;
   $emp2 -> first_name = 'Gireesh';
   $emp2 -> last_name = 'Amara';
   $emp2 -> country = 'Australia';

   print_employee($emp1);
   print_employee($emp2);
?>

 

Output

$./instance_properties_demo.php 


id : 1
first_name : Krishna
last_name : Gurram
country : India

id : 2
first_name : Gireesh
last_name : Amara
country : Australia

 

 

 

 

 

Previous                                                    Next                                                    Home

Thursday, 6 May 2021

Php: How to get an object from class?

You can get an object using new keyword followed by class name.

 

Syntax

$obj1 = new ClassName;

$obj2 = new ClassName(arguments);

 

Let’s try 1st syntax, I will explain second syntax in upcoming posts.

 

new_instance_demo.php

#!/usr/bin/php

<?php

   class Employee{

   }

   $emp1 = new Employee;

   echo '$emp1 is an instance of ' . get_class($emp1);
?>

 

Output

$./new_instance_demo.php 

$emp1 is an instance of Employee

 


 

Previous                                                    Next                                                    Home

jq: . : Access value of specific property

Using . operator, we can access specific value of a property.

 

empPretty.json 

{
  "org": [{
    "name": "Honeywell",
    "yrsOfExperience": 2.2
  }, {
    "name": "IBM",
    "yrsOfExperience": 1.8
  }],
  "firstName": "Krishna",
  "lastName": "Ram",
  "address": {
    "city": "Bangalore",
    "country": "India"
  }
}

 

.org: access the value of org property

.firstName: access the value of firstName property

.lastName: access the value of lastName property

.address.city access the value of city property.

 

$cat empPretty.json | jq '.org'
[
  {
    "name": "Honeywell",
    "yrsOfExperience": 2.2
  },
  {
    "name": "IBM",
    "yrsOfExperience": 1.8
  }
]
$
$
$cat empPretty.json | jq '.firstName'
"Krishna"
$
$cat empPretty.json | jq '.lastName'
"Ram"
$
$cat empPretty.json | jq '.address.city'
"Bangalore"

 

Access multiple properties in single command

Using , operator, you can access multiple properties in single command.

 

Example

$cat empPretty.json | jq '.firstName, .lastName'
"Krishna"
"Ram"

Above command extract firstName and lastName properties.

$cat empPretty.json | jq '.org[].name, .org[].yrsOfExperience'
"Honeywell"
"IBM"
2.2
1.8


Above command extract organization name and yrsOfExperience of an employee.



Previous                                                    Next                                                    Home

Wednesday, 5 May 2021

Php: class_exists: Check whether class is defined or not

Signature

class_exists ( string $class_name , bool $autoload = true ) : bool

 

Description

Checks if the class has been defined

 

employee.php

<?php

class Employee{
    
}
?>

 

class_exist_demo.php

#!/usr/bin/php

<?php

    $is_exist = class_exists('Employee');
    echo "is Employee exist : ";
    var_dump($is_exist);

    include 'employee.php';

    $is_exist = class_exists('Employee');
    echo "\nis Employee exist : ";
    var_dump($is_exist);

?>

 

Output

$ ./class_exist_demo.php 

is Employee exist : bool(false)

is Employee exist : bool(true)

 

 

 

 

  

Previous                                                    Next                                                    Home