Wednesday, 5 May 2021

jq: -S: sort keys of objects on output

-S option is used to sort the keys of objects on jq output.

 

empDetails.json 

{"org":[{"name":"Honeywell","yrsOfExperience":2.2},{"name":"IBM","yrsOfExperience":1.8}],"firstName":"Krishna","lastName":"Ram"}

 

By default, jq command display json as it is.

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

 

If you want to sort the json output using the keys, use -S’ option.

$cat empDetails.json | jq -S
{
  "firstName": "Krishna",
  "lastName": "Ram",
  "org": [
    {
      "name": "Honeywell",
      "yrsOfExperience": 2.2
    },
    {
      "name": "IBM",
      "yrsOfExperience": 1.8
    }
  ]
}

 

 

 

 

Previous                                                    Next                                                    Home

Tuesday, 4 May 2021

jq: Pretty print json

In general, we can invoke ‘jq’ command by providing a piece of JSON to its standard input.

 

For example,

echo '{"org":[{"name":"Honeywell","yrsOfExperience":2.2},{"name":"IBM","yrsOfExperience":1.8}],"firstName":"Krishna","lastName":"Ram"}' | jq

 

Above command pass the output of echo as input to ‘jq’ command. 

$echo '{"org":[{"name":"Honeywell","yrsOfExperience":2.2},{"name":"IBM","yrsOfExperience":1.8}],"firstName":"Krishna","lastName":"Ram"}' | jq 
{
  "org": [
    {
      "name": "Honeywell",
      "yrsOfExperience": 2.2
    },
    {
      "name": "IBM",
      "yrsOfExperience": 1.8
    }
  ],
  "firstName": "Krishna",
  "lastName": "Ram"
}

 

As you see the output, jq command formats the json.

 

You can even keep the json in a file and pass it as input to jq command.

 

empDetails.json

{"org":[{"name":"Honeywell","yrsOfExperience":2.2},{"name":"IBM","yrsOfExperience":1.8}],"firstName":"Krishna","lastName":"Ram"}

 

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

 

 

 

 

 

Previous                                                    Next                                                    Home

Php: get_declared_classes: Get all the classes that Php knows

Signature:

get_declared_classes ( ) : array

 

Description

Returns an array with the name of the defined classes.

 

Example

$declared_classes = get_declared_classes();

 

Find the below working example.

 

employee.php

<?php

class Employee{
    
}
?> 

student.php

<?php

class Student{
    
}
?> 


get_declared_classes_demo.php

#!/usr/bin/php

<?php

    include 'employee.php';
    include 'student.php';

    $declared_classes = get_declared_classes();

    print_r($declared_classes);
?> 


Output

$ ./get_declared_classes_demo.php 

Array
(
    [0] => stdClass
    [1] => Exception
    [2] => ErrorException
    [3] => Error
    [4] => CompileError
    [5] => ParseError
    [6] => TypeError
    [7] => ArgumentCountError
    [8] => ArithmeticError
    [9] => DivisionByZeroError
    [10] => Closure
    [11] => Generator
    [12] => ClosedGeneratorException
    [13] => DateTime
    [14] => DateTimeImmutable
    [15] => DateTimeZone
    [16] => DateInterval
    [17] => DatePeriod
    [18] => LibXMLError
    [19] => SQLite3
    [20] => SQLite3Stmt
    [21] => SQLite3Result
    [22] => CURLFile
    [23] => DOMException
    [24] => DOMStringList
    [25] => DOMNameList
    [26] => DOMImplementationList
    [27] => DOMImplementationSource
    [28] => DOMImplementation
    [29] => DOMNode
    [30] => DOMNameSpaceNode
    [31] => DOMDocumentFragment
    [32] => DOMDocument
    [33] => DOMNodeList
    [34] => DOMNamedNodeMap
    [35] => DOMCharacterData
    [36] => DOMAttr
    [37] => DOMElement
    [38] => DOMText
    [39] => DOMComment
    [40] => DOMTypeinfo
    [41] => DOMUserDataHandler
    [42] => DOMDomError
    [43] => DOMErrorHandler
    [44] => DOMLocator
    [45] => DOMConfiguration
    [46] => DOMCdataSection
    [47] => DOMDocumentType
    [48] => DOMNotation
    [49] => DOMEntity
    [50] => DOMEntityReference
    [51] => DOMProcessingInstruction
    [52] => DOMStringExtend
    [53] => DOMXPath
    [54] => HashContext
    [55] => finfo
    [56] => LogicException
    [57] => BadFunctionCallException
    [58] => BadMethodCallException
    [59] => DomainException
    [60] => InvalidArgumentException
    [61] => LengthException
    [62] => OutOfRangeException
    [63] => RuntimeException
    [64] => OutOfBoundsException
    [65] => OverflowException
    [66] => RangeException
    [67] => UnderflowException
    [68] => UnexpectedValueException
    [69] => RecursiveIteratorIterator
    [70] => IteratorIterator
    [71] => FilterIterator
    [72] => RecursiveFilterIterator
    [73] => CallbackFilterIterator
    [74] => RecursiveCallbackFilterIterator
    [75] => ParentIterator
    [76] => LimitIterator
    [77] => CachingIterator
    [78] => RecursiveCachingIterator
    [79] => NoRewindIterator
    [80] => AppendIterator
    [81] => InfiniteIterator
    [82] => RegexIterator
    [83] => RecursiveRegexIterator
    [84] => EmptyIterator
    [85] => RecursiveTreeIterator
    [86] => ArrayObject
    [87] => ArrayIterator
    [88] => RecursiveArrayIterator
    [89] => SplFileInfo
    [90] => DirectoryIterator
    [91] => FilesystemIterator
    [92] => RecursiveDirectoryIterator
    [93] => GlobIterator
    [94] => SplFileObject
    [95] => SplTempFileObject
    [96] => SplDoublyLinkedList
    [97] => SplQueue
    [98] => SplStack
    [99] => SplHeap
    [100] => SplMinHeap
    [101] => SplMaxHeap
    [102] => SplPriorityQueue
    [103] => SplFixedArray
    [104] => SplObjectStorage
    [105] => MultipleIterator
    [106] => JsonException
    [107] => SessionHandler
    [108] => __PHP_Incomplete_Class
    [109] => php_user_filter
    [110] => Directory
    [111] => AssertionError
    [112] => PDOException
    [113] => PDO
    [114] => PDOStatement
    [115] => PDORow
    [116] => PharException
    [117] => Phar
    [118] => PharData
    [119] => PharFileInfo
    [120] => ReflectionException
    [121] => Reflection
    [122] => ReflectionFunctionAbstract
    [123] => ReflectionFunction
    [124] => ReflectionGenerator
    [125] => ReflectionParameter
    [126] => ReflectionType
    [127] => ReflectionNamedType
    [128] => ReflectionMethod
    [129] => ReflectionClass
    [130] => ReflectionObject
    [131] => ReflectionProperty
    [132] => ReflectionClassConstant
    [133] => ReflectionExtension
    [134] => ReflectionZendExtension
    [135] => mysqli_sql_exception
    [136] => mysqli_driver
    [137] => mysqli
    [138] => mysqli_warning
    [139] => mysqli_result
    [140] => mysqli_stmt
    [141] => SimpleXMLElement
    [142] => SimpleXMLIterator
    [143] => SNMP
    [144] => SNMPException
    [145] => SoapClient
    [146] => SoapVar
    [147] => SoapServer
    [148] => SoapFault
    [149] => SoapParam
    [150] => SoapHeader
    [151] => tidy
    [152] => tidyNode
    [153] => XMLReader
    [154] => XMLWriter
    [155] => XSLTProcessor
    [156] => Employee
    [157] => Student
)


From the output, you can confirm that apart from built-in classes, Employee and Student classes are also known to Php.




 

 

Previous                                                    Next                                                    Home

Monday, 3 May 2021

Php: How to define a class?

Class defines a prototype of an object. Class is defined using 'class' keyword followed by class name, class definition is surrounded in curly braces.

 

Syntax

class ClassName{

        

}

 

Example 1

class Employee{

        

}

 

Example 2

class UserUtil{

        

}

 

As per the convention,

a.   Class name starts with a capital letter and every 1st character of subsequent words also a capital letter.

b.   Define each class in a separate file

Previous                                                    Next                                                    Home

Install and setup jq

On Linux

sudo apt-get install jq

sudo dnf install jq

sudo zypper install jq

sudo pacman -S jq

 

On Mac

brew install jq

port install jq

 

On FreeBSD

pkg install jq

 

On Solaris

pkgutil -i jq

 

You can directly download jq binaries from below location and add the binary to system path and use.

https://stedolan.github.io/jq/download/

 

You can even built jq binaries from source, go through following link for more details.

https://stedolan.github.io/jq/download/

 

Test setup

Once you install jq in your system, open terminal and execute ‘jq’ command.

$jq
jq - commandline JSON processor [version 1.6]

Usage:	jq [options] <jq filter> [file...]
	jq [options] --args <jq filter> [strings...]
	jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Example:

	$ echo '{"foo": 0}' | jq .
	{
		"foo": 0
	}

For a listing of options, use jq --help.

 

 

 

 

Previous                                                    Next                                                    Home

Introduction to jq

‘jq’ is a command line json processing tool written in C language. jq is used for processing JSON inputs, applying the given filter to its JSON text inputs and producing the filter's results as JSON on standard output.

 

Is ‘jq’ cross platform?

Yes, ‘jq’ is available in platforms Linux, OS X and Windows.

 

Is ‘jq’ open source?

Yes, ‘jq’ is available with  MIT license.

Previous                                                    Next                                                    Home

jq: Json Processing tool tutorial

      Introduction to jq
      Install and setup jq
      Pretty print json
      -S: sort keys of objects on output
      . : Access value of specific property
      []: Navigate array
      Processing curl command output
      Processing wget command output
      pipe : combine two filters
      functions
      Get all the firstnames of employees from json document
      Comparison operators
      Complex queries example
      Convert integer to a string
      Select the object based on a property value


Previous                                                    Next                                                    Home