I have a shell script from where I want to access value of a Json key, using regex. I know we should use JQ for it but the problem is, in my app, the format of the file is not fixed, it can be either xml or Json so for that reason i want to use regex. So to script I will be passing two things, filename and regex command.
Now the problem is I have no idea how I can fetch particular key data using regex.
abc.json
{
"name": "marsh",
"address":[{"house_no":123,"Landmark":"Opp. to starbucks","mobile_no":{}}]
}
Now I want to just fetch the value of house_no in a variable and Landmark in another variable but I don't know how I can achieve that. I have been using jq for fetching json data in script but now i want to fetch using regex
The filename will be abc.json and regx command is something that i want to know My shell script is
house_num=$(cat abc.json | jq -r '.name')
so I want it to be converted to
house_num = $(cat $filename | sed $regx_command) mobile_num = $(cat $filename | sed $regx_command)
or
house_num = $(cat $filename | grep $regx_command) mobile_num = $(cat $filename | sed $regx_command)
I tried few regex but they were returning the whole address array. I don't want that I want the house_num to have value as 123 only.
Is there any way we can achieve this ? PS- I have to use regex as that is the requirement.
I tried few regex. something like this But I am getting a lot of matches there and not just a single value. I am very much new to all of this so, getting confused.
I tried this
/.*?"id": (\d+).*?("exclusions": \[.*?|"desig").*?{/gs
on this json
{
"id": 5229,
"Type": B,
"exclusions": [
{
"id": 3486,
"desig": "A",
"price": 101,
},
{
"id": 3489,
"desig": "A",
"price": 101,
}]},
{
"id": 5239,
"Type": B,
"exclusions": []
}
But it is matching with bunch of values and not just value of desig