0

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

1 Answers1

0

great to see that you have recently picked up asking questions! i would highly recommend you check out this post: https://stackoverflow.com/help/how-to-ask

i am not really sure on your concrete question, since there are multiple questions with seemingly no correlation to another in your post. thats why i will just answer the questions i understand and hope u can give me some feedback on whats missing and if i was able to help you :)

Q1: getting values of house_no and Landmark in json

i would advice you to use two different match groups, one for the house_no and one for the Landmark value. This way you can clearly specify your matching condistions for each and just have to concatinate them with an OR ( | ).

for matching a specific value you should use lookaheads and lookbehinds. with these you can clearly specify what your matching conditions are, without the entire match beeing included in the result.

as a lookahead i would use "house_no" and "Landmark" as references to get the correct values for and as a lookbehind i would use a negative character match clause, which matches all characters, except the one specified.

combining these approaches lead me to the following regex:

((?<="house_no":)[^,]+)|((?<="Landmark":")[^"]+)

Q2: matching the value of desig

using the same methodogy as in Q1, we get the following regex:

((?<="desig": ")[^"]+)

hope i could help you, please mark this post as the answer to your question if thats the case :)

A-New-User
  • 61
  • 3