Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Bash, get value from json line

Writer Matthew Martinez

System

Linux local 5.0.0-27-lowlatency #28-Ubuntu SMP PREEMPT Tue Aug 20 20:33:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Issue

I have input file.

{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}

How can I get only this output (some cols)?

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"

No jq or other apps, I need just bash sed or awk, or some basic commands, I don't have admin rights to install it.

I tried

I tried cut, but there is a problem with delimiter , (sometimes are in ").

Thanks.

2

3 Answers

You can try this:

grep -o '"[^"]*"\s*:\s*"[^"]*"' | \
grep -E '^"(code|name|is_action|action_from|action_to)"' | \
tr '\n' ',' | \
sed 's/,$//'

Details:

  • grep -o '"[^"]*"\s*:\s*"[^"]*"' find all "key": "value" pairs and prints them on separate lines;

Example:

echo '{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}' | grep -o '"[^"]*"\s*:\s*"[^"]*"'

Output:

"code": "01333457004"
"name": "ALAZANIS VALLEY 2015"
"note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ"
"sel_unit": "Kus"
"category": "ČERVENÉ,POLOSLADKÉ"
"unit": "L"
"EAN": "4867601700052"
"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;"
"is_action": "1"
"action_from": "20190905"
"action_to": "20190918"
"ordered_from": "20190126"
"ordered_to": "20190830"
"shelf_id": "1030542"
  • grep -E '^"(code|name|is_action|action_from|action_to)"' filters only needed keys.

Output:

"code": "01333457004"
"name": "ALAZANIS VALLEY 2015"
"is_action": "1"
"action_from": "20190905"
"action_to": "20190918"
  • tr '\n' ',' replace new line to comma.

Output:

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918",
  • sed 's/,$//' removes last extra comma.

Output:

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"

Full example

Command:

echo '{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}' | grep -o '"[^"]*"\s*:\s*"[^"]*"' | grep -E '^"(code|name|is_action|action_from|action_to)"' | tr '\n' ',' | sed 's/,$//'

Output:

"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"
4
#!/bin/bash
declare -A JSON
a=$( grep -Po '\{.*\{\K(.*?)(?=\}\})' example.json | grep -Po '(^|,)\"\K(.*?)(?=\":)'
)
for b in $a; do JSON[$b]="$(grep -Po "\"$b\": \"?\K.*?(?=(\"?,\"|\}\}))" example.json)"
done
echo ${JSON[@]}
echo ${!JSON[@]}
echo ${JSON[name]}

or if you just want the values:

grep -Po '\{.*\{\K(.*?)(?=\}\})' example.json | grep -Po "\".*?\": \"?\K.*?(?=(\"?,\"|$))"

Although the , delimiter fails, you can use : delimiter with cut:

#!/bin/bash
# NAME: ParseJSON
# PATH: $HOME/askubuntu/
# DESC: Answer for:
# DATE: September 16, 2019
Line='{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}'
Code=$(echo "$Line" | cut -d':' -f3)
Code="${Code%,*}" # Remove ,"name"
Name=$(echo "$Line" | cut -d':' -f4)
Name="${Name%,*}" # Remove ,"note"
IsAction=$(echo "$Line" | cut -d':' -f15)
IsAction="${IsAction%,*}" # Remove ,"action_from"
ActionFrom=$(echo "$Line" | cut -d':' -f16)
ActionFrom="${ActionFrom%,*}" # Remove ,"action_to"
ActionTo=$(echo "$Line" | cut -d':' -f17)
ActionTo="${ActionTo%,*}" # Remove ,"ordered_from"
echo '"code":'"$Code,"'"name":'"$Name,"'"is_action":'"$IsAction,"'"action_from":'"$ActionFrom,"'"action_to":'"$ActionTo"

I called the script ParseJSON and this is it's output:

$ ParseJSON
"code": "01333457004","name": "ALAZANIS VALLEY 2015","is_action": "1","action_from": "20190905","action_to": "20190918"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy