Ragini
Ragini

Reputation: 29

How to use jq to get a value of decimal/number type from a JSON response which is not surrounded by " "

I am new to shell scripting and I need some help. I am trying to use jq to get values from a api response and check for its correctness.

Here is a sample for how the response looks like,

    {
      "data" : {
        "transactionType" : "Sales",
        "transactionSubType" : "DomesticSale",
        "Items" : [ {
          "itemID" : "2",
          "itemType" : "Good",
          "amount" : 5.0,
          "tax" : 1.0        
        } ]
     }
}

I am able to get the values for transactionType or transactionsubtype or even ItemID values etc as given below

jq '.data.transactionType'    
jq '.data.Items[0].itemID' 

for Transaction type and item id

but when it comes to values of numeric types i.e., without the quotes in it, I don't get any value. I am using similar syntax for the numeric type also as shown below.

jq '.data.Items[0].amount'
jq '.data.Items[0].tax'

Please help!!!

Upvotes: 1

Views: 403

Answers (1)

peak
peak

Reputation: 116900

Your jq invocations are fine, but the sample data is missing a final closing brace ("}"), so perhaps you were not feeding jq properly.

If you're wondering why you didn't see an error message, it's almost certainly because jq 1.5 is not very good about handling incomplete JSON. The problem has since been fixed at "master". With the current version, you'd see something like this:

parse error: Unfinished JSON term at EOF at line 15, column 0

Upvotes: 3

Related Questions