0

I have JSON file named input.json wich contains following data


{
 "Data": {
   "Info": [
     {
       "tranID": "1",
       "architecture": "thread_1",
       "build": "110",
       "win": "450"
     },
     {
       "tranID": "2",
       "architecture": "thread_2",
       "build": "120",
       "win": "455"
     }
   ]
 }
}

Here I want to write function which will get file name as input parameter and now inside function I want to read last Object array and two fields from which build and win ans stor there values is variable ex total which will contain addition of build and win and insert this value again inside JSON .

so that final JSON will look like


 {
  "Data": {
    "Info": [
      {
        "tranID": "1",
        "architecture": "thread_1",
        "build": "110",
        "win": "450"
      },
      {
        "tranID": "2",
        "architecture": "thread_2",
        "build": "120",
        "win": "455",
       "total": "575"
      }
    ]
  }
}

I have tried reading initial JSON build fiels and tried to store it in variable just printed that variable but variable is not stroing that value it's coming empty .

varr =  $jq '.Data.Info[-1].build' ${1}
echo $varr 

also tried

varr=$(echo $jq '.Data.Info[-1].build' ${1})
echo $varr 

echo not printing value

where ${1} is my input JSON file

Ashu007
  • 19
  • 4
  • 1
    If `jq '.Data.Info[-1].build' ${1}` is the command you've already found to be working, then `varr=$(jq '.Data.Info[-1].build' ${1})` with no echo or anything – that other guy Apr 18 '20 at 19:11
  • 1
    Don't you hate it when a question is closed as you're about to hit submit on an answer... This should help you get started: `jq '.Data.Info[-1] as $last | .Data.Info[-1].total = (($last.build | tonumber) + ($last.win | tonumber) | tostring)' input.json` -- basically, do all the math and such *in* `jq`. Save its output to a temporary file and then `mv` that back to your original. – Shawn Apr 18 '20 at 19:11
  • 1
    Lots of bugs here around inadequate quoting, BTW. Might make a habit of running your code through http://shellcheck.net/ before asking questions here. – Charles Duffy Apr 18 '20 at 19:35
  • Thank You for help – Ashu007 Apr 19 '20 at 16:37

0 Answers0