Making JSON more readable in the Terminal with Curl extension jq

jq banner

When developing apps and games you may need to read JSON files quickly in the Terminal, this can be a pain.

JSON is a right pain to read in the Mac terminal but Cloudant have some great tips for viewing JSON here.

First of all here is a random JSON file displayed in the mac terminal using curl.

Terminal Command:

1
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'

json default

What a mess.

Solution

Install the ./jq lightweight command line JSON processor from http://stedolan.github.io/jq/

Install (type this in the terminal and install)

1
2
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install jq

fyi: Homebrew is a packaged installer (read more http://brew.sh/) and check out http://braumeister.org/ to see what other packages you can install.

Conclusion

Now lets try the curl command again with ” ! jq .” added to the end of the curl request.

Terminal Command:

1
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq .

color coded json in terminal

If you just want a web based tool try the Postman extension for Google Chrome.

Programming Logic and Basic Programming terms

Programming logic is a quick description of the flow of your program.
A very simple program logic may be

    START MAIN
    CHECK ITEM_PRICE
    CHECK BANK_BALANCE
    IF ITEM_PRICE < BANK_BALANCE THEN
      BUY_ITEM

    END IF
    END MAIN

Before we write the program above we need to identify what is data and what is language associated with the programming language.

Data would be BANK_BALANCE (that is a number) and ITEM_PRICE is a number too.

“IF” and “END IF” are commands available in this program. When dealing with data you can assign the data to a variable name, for example your bank balance (a number) can be saved in the variable MyBankBalance and your credit card balance can be saved in a variable called MyCreditCardBalance.

There are different types of variables used in Visual Studio 2010 – Visual Basic.

    Bits: 0 or 1 (True or False)
    Byte: -128 to 127
    String: any text you can think of.
    Number variables:
    Short: -32,768 through 32,767.
    Long: -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
    Decimal: -79,228,162,514,264,337,593,543,950,335 through 79,228,162,514,264,337,593,543,950,335

There are other types but you can read about them here: http://msdn.microsoft.com/en-us/library/ae55hdtk.aspxIf you have to choose one type of variable choose a string as it can hold almost anything.

With numbers try and use the variable with the smallest maximum to save using too much memory (e.g. If you were creating a variable to hold ones age use a Byte, if you were creating a variable to hold a bank balance use a Decimal over an integer mostly because Decimals allow decimal points where Integers round off anything after a decimal point.

Now what we have our programming logic decided our programming variables how can we optimize my program. Let’s say we want to check our primary bank balance then our secondary bank balance if the primary bank balance does not have enough money. If we duplicated the code that checks the bank balance that would work but the code would get twice as big. What we need to do is move the code that checks the bank balance into a FUNCTION called “CHECK_BANK_BALANCE” and pass in the name of the bank account to check. No we can check 1, 2 or 2000 bank balances without duplicating code.

The new code might look like

    START MAIN
    CHECK ITEM_PRICE
    IF ITEM_PRICE < CHECK_BANK_BALANCE(BANK_ACCOUNT_NUMBER) THEN
      BUY_ITEM

    END IF
    END MAIN

    FUNCTION CHECK_BANK_BALANCE(BANK_ACCOUNT_NUMBER)

      RETURN BANK_BALANCE

    END FUNCTION

The code above is not perfect, we do not have a breakdown of the check price function but pseudo code allows you to flesh out the structure of your program before you start coding.

Another alternative to using a function may be to use a “Sub” (Subroutine), A subroutine is like a function but it does not return any data. You could use a sub routine to deduct an amount from a bank balance and not return the bank balance when done.

CONCLUSION:

  • Pseudo code allows you to flesh out your program. If you have an idea just write our the pseudo code, review and optimise it. When you are happy with the flow, choose a suitable software language and start turning the pseudo code into real code.
  • Variables are used to hold data.
  • There are variables for text, numbers etc.
  • Use FUNCTIONS instead of duplicating code.

Programming 101

Before you can learn how to program you need to understand what a computer is, a computer at its simplest form is a really fast calculator that can only do one thing at a time. Technically it can do more than one thing at a time but we will come to that discussion at a later date.

In the 1940’s computers and electronics were very basic, there was no internet, no mobile phones or iPods. The world was at war and the Allies and NAZI’s were busy designing computers to assist with encrypting war messages or to accurately predict artillery accuracy. One of the first programmable computers (ENIAC) was developed by the US during WW2 to help calculate artillery firing tables. The ENIAC was able to perform calculations in hours that previously took days. The ENIAC was 100% electronic, the NAZI’s were designing a mix of electronic and mechanical computers (but lucky for us the allies blew it up).

Moving forward to the 1960’s NASA was planning on sending humans to the moon, they needed a way to make computers on the Apollo spacecraft more reliable. NASA contractors files patent US3517171 http://www.google.com/patents/US3517171 for a “Self testing and repairing computer”. This patent allowed computers to interrupt a running program and start doing something else more important. Without the benefit of this invention most computers would still be boring calculators running batches of calculations.

So you may be asking when do we stop learning about history and start programming?

    Ok in the next 5 minutes we will create a program to create a new thread, initialize the TCP stack, create a socket to send an ICMP packet to ping a server after returning the context to the current thread, but when the ICMP reply returns we will invoke the current thread via a delegate and update the UI.

Just kidding, we will not be doing that for some time to come. We will do some fun stuff before that.

Going back to our quick history lesson, The WW2 allies and NASA had to learn how to write software, we have to master the same concepts before we can program. We need to understand what processor is and what memory is.

A processor is a silicon chip that performs calculations really fast, it is the heart of your computer/iPad/Laptop. Processors are quite powerful these days but they still have to read our calculations from memory and write the results back to memory. Memory is basically a huge empty storage area for data. When you turn on your computer its memory is completely empty until your operating system or software writes data to the memory. Software can read and write anything to memory (images, videos, this blog post or games).

Processors speed are measured in Mhz and Memory is measured in Megabytes. I am using a processor with 4 processors running at 3,400,000,000 Mhz, on any good second I could calculate 3 billion calculations a second.

Most x86 compatible (Intel or AMD) processors use commands like ADD, DIV, SUB, MUL, OR, AND CALL, to calculator or evaluate data. Instructions like Divide are truncated to “DIV” and Multiply is shortened to “MUL”. Lucky we do not call these commands directly, we have fancy programming development environments to do all the hard work for us. See the full instruction set here http://en.wikipedia.org/wiki/X86_instruction_listings

Conclusion

  • A computer is a calculator, we have to tell it what to do.
  • A processor can only read from memory, calculate and write the result back to memory.

New Programming Blog Category

I have decided to add a blog category for programming, yes computer programming. Programming is a very creative and fun thing to do. You could program a website, program a bath file to manage your files or program a software program to read you the outside temperature.

Far too many school students are leaving school having studied programming but could not explain 5 computer variable types. That would be like asking a keen cook to name 5 cooking ingredients that you could use in a sweet and savoury dish.

Over the next few months please join me on a journey for basic programming.

Topics:

  • Programming basics.
  • Different types of programming.
    • Windows Software Programming.
    • Webpage programming.
    • Database programming.
    • iPhone/iPad and Android Programming.

I will hope to demonstrate programming using as much free software as possible. Worst case you may require a personal website but that is a cheap as $5 a month (via a host like http://www.jumba.com.au/hosting/personal).

Show me the programming posts.