Tuesday, October 25, 2022

[SOLVED] Extract values from JSON object using standard utilities

Issue

I have a txt file and its content is as follows. I am trying leave only value like 12, [email protected] and delete brackets, quotation mark, comma, and the key. Could someone help me what I miss or go wrong with my command line? Thank you!

{
"id": 12,
"email": "[email protected]",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg",
}

Now, the result I can output is that

{
    12,
    "[email protected]",
    "Rachel",
    "Howell",
    "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg",
 }

What I expected is:

12
[email protected]
Rachel
Howell
https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg

Here is my command:

sed -e 's/[{]//g' | cut -d':' -f '2' test.txt

Solution

If you only wanna use sed cut and grep, I would recommand you that :

sed "s/}//g;s/{//g;s/\"//g;s/,//g;s/\ //g" test.txt | cut -d":" -f2 | egrep "..*"

But there's way more officient.

--

EDIT : description of the command :

sed "s/}//g;s/{//g;s/\"//g;s/,//g;s/\ //g"

-> removes {}",

cut -d":" -f2

-> only takes the 2nd part

egrep "..*"

-> only takes the lines which contain something



Answered By - ATR
Answer Checked By - Marie Seifert (WPSolving Admin)