简体   繁体   中英

How can i parse through a json file in a bash script?

I want to grab the faceRectangle and and place that into another text file like so

SET "faceRectangle" ""top": 114"

SET "faceRectangle" ""left": 212"

So that all the values under there corresponding heading are properly formatted and copied into another file like above and i want to do this in a bash file

[
  {
"faceRectangle": {
  "top": 114,
  "left": 212,
  "width": 65,
  "height": 65
},
"scores": {
  "anger": 1.0570484E-08,
  "contempt": 1.52679547E-09,
  "disgust": 1.60232943E-07,
  "fear": 6.00660363E-12,
  "happiness": 0.9999998,
  "neutral": 9.449728E-09,
  "sadness": 1.23025981E-08,
  "surprise": 9.91396E-10
}
 }

]

how would I go about doing this?

UPDATE: this is what I have so far:

#!/bin/bash

faceR=($(jq -r '.[0].faceRectangle' emotion.json))
scores=($(jq -r '.[0].scores' emotion.json))

echo " SET "faceRectangle" "${faceR[@]}" " >> data.txt

cat data.txt | redis-cli --pipe

I keep getting an error "unbalanced quotes in request" how can I fix that?

This ouputs what you were going for:

#!/usr/bin/env bash

top=($(jq -r ".[0].faceRectangle.top" emotion.json))
left=($(jq -r ".[0].faceRectangle.left" emotion.json))

echo "SET \"faceRectangle\" \"top\": ${top}\""
echo "SET \"faceRectangle\" \"left\": ${left}\""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM