Skip to main content
Bumped by Community user
Mostly correction of markdown
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

I'm using a bash script that needs to read the JSON output and parse a value from different JSON variables or strings. Here's the sample output. It needs to read the value next to the Content or from any other variable. Such as,

Lookup Content and be able to print Value1.

Lookup DeviceType and be able to print Value4.

Sample OuputInput: {"Content":"Value1","CreationMethod":"Value2","database":"Value3","DeviceType":"Value4"}

{"Content":"Value1","CreationMethod":"Value2","database":"Value3","DeviceType":"Value4"}

I tried the combination of sed and awk sed 's/["]/ /g' | and awk '{print $4}',:

sed 's/["]/ /g' | awk '{print $4}'

... but only if the position of Content remains the same in the output. otherwiseOtherwise, in the different JSON output, the positioning of Content changes that puts the value out of scope thus  , awk '{print $4}' picks up the wrong value.

I'm using bash script that needs to read the JSON output and parse a value from different JSON variables or strings. Here's the sample output. It needs to read the value next to the Content or from any other variable. Such as,

Lookup Content and be able to print Value1.

Lookup DeviceType and be able to print Value4

Sample Ouput: {"Content":"Value1","CreationMethod":"Value2","database":"Value3","DeviceType":"Value4"}

I tried the combination of sed and awk sed 's/["]/ /g' | awk '{print $4}', but only if the position of Content remains the same in the output. otherwise in the different JSON output, the positioning of Content changes that puts the value out of scope thus  awk '{print $4}' picks up the wrong value.

I'm using a bash script that needs to read the JSON output and parse a value from different JSON variables or strings. Here's the sample output. It needs to read the value next to the Content or from any other variable. Such as,

Lookup Content and be able to print Value1.

Lookup DeviceType and be able to print Value4.

Sample Input:

{"Content":"Value1","CreationMethod":"Value2","database":"Value3","DeviceType":"Value4"}

I tried the combination of sed and awk:

sed 's/["]/ /g' | awk '{print $4}'

... but only if the position of Content remains the same in the output. Otherwise, in the different JSON output, the positioning of Content changes that puts the value out of scope thus, awk '{print $4}' picks up the wrong value.

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264
Source Link
Riz
  • 59
  • 1
  • 2
  • 4

Parse value from different JSON strings (No jq)

I'm using bash script that needs to read the JSON output and parse a value from different JSON variables or strings. Here's the sample output. It needs to read the value next to the Content or from any other variable. Such as,

Lookup Content and be able to print Value1.

Lookup DeviceType and be able to print Value4

Sample Ouput: {"Content":"Value1","CreationMethod":"Value2","database":"Value3","DeviceType":"Value4"}

I tried the combination of sed and awk sed 's/["]/ /g' | awk '{print $4}', but only if the position of Content remains the same in the output. otherwise in the different JSON output, the positioning of Content changes that puts the value out of scope thus awk '{print $4}' picks up the wrong value.