0

I have a .json file containing the following parameters:

{
  "account_ref" : "dh2fk3weh28dnd",
  "fname" : "john",
  "sname" : "smith",
  "address" : "42 test street",
  "product" : "all",
  "email" : "[email protected]",
  "tele" : "0384756783"
}

This file will be on a linux server and I need find a way to extract the account_ref value and insert it into a csv file using automation.

I have no experience in this scripting, regex and I believe that regex may be the answer to extracting the account_ref value. Any advice is welcome. Thanks.

1
  • If you're going to work with a lot of JSON in your linux CLI or scripts, I've heard jq is the best tool for that. You might want to check it out. Commented Jul 13, 2016 at 12:06

1 Answer 1

1

";" is csv delimiter

qa.json is file with you data

output.csv is result csv

$ cat qa.json

{ "account_ref" : "dh2fk3weh28dnd", "fname" : "john", "sname" : "smith", "address" : "42 test street", "product" : "all", "email" : "[email protected]", "tele" : "0384756783", }

$ cat qa.json | awk -F'\"' '{for (i=1; i<=NF; i++) { if ((i%2)==0) { if($i=="account_ref"){ print $i ";" $(i+2)}}}}' > output.csv

or

awk -F'\"' '{for (i=1; i<=NF; i++) { if ((i%2)==0) { if($i=="account_ref"){ print $i ";" $(i+2)}}}}' qa.json  > output.csv

$ cat output.csv

account_ref;dh2fk3weh28dnd

Sign up to request clarification or add additional context in comments.

1 Comment

how about sed -r -n 's/.*(account_ref).*:.*"(.*)".*/\1;\2/p' qa.json ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.