Akshay Salvi
Akshay Salvi

Reputation: 71

Extract a value from a nested json key in bash

Consider I have the below JSON:

{
  "id": "Ab12",
  "details": "{\"timeValue\":null,\"lastModifiedIn\":\"PHX\"}",
  "version": 3
}

I want to extract the value of 'lastModifiedIn' key without using the jq command. Basically, the result I seek for is 'PHX'.

Is there a way to extract this using basic shell scripting?

Upvotes: 1

Views: 1710

Answers (1)

Ed Morton
Ed Morton

Reputation: 203645

It's sloppy but given the input and your requirement to not use jq this might be good enough for your purposes:

$ sed -n 's/.*"lastModifiedIn\\":\\"\([^\\]*\).*/\1/p' file
PHX

Upvotes: 4

Related Questions