1

I need to parse JSON using shell script (I used to use Python, but not allowed in this specific environment), in this example, I want to get the value of b1, which is 6.5, anyone have any experience?

Using Linux/Mac OSX.

{
    "value": {
        "a1": {
            "amount": "5.5",
            "currency": "USD"
        },
        "b1": {
            "amount": "6.5",
            "currency": "USD"
        },
        "c1": {
            "d1": {
                "amount": "7.5",
                "currency": "USD"
            },
            "e1": {
                "amount": "8.5",
                "currency": "USD"
            },
            "f1": {
                "amount": "9.5",
                "currency": "USD"
            }
        },
        "g1": {
            "amount": "10.5",
            "currency": "USD"
        },
    }
}
9
  • 1
    github.com/dominictarr/JSON.sh Commented May 11, 2016 at 23:03
  • 1
    or github.com/step-/JSON.awk if you have AWK Commented May 11, 2016 at 23:03
  • 1
    but AFAIR oython is a standart component of any OS X installation, isn't it? Commented May 11, 2016 at 23:04
  • 1
    You don't need to install anything with NPM. Just look at test/parse-test.sh. All you need is to create a shell script which includes JSON.sh via source (== .) command and then extracts necessary data. Just two files created in your home dir. To use an instantly ready library with a decent parser or to stay with a regular expression — well, it's your choice, indeed. If you or your customer plan to perform any further development — I'd stay with the library. Commented May 12, 2016 at 0:12
  • 1
    Well, I haven't added any new information. Google already gives JSON.sh as one of the first choices for "JSON shell parser" query. So I doubt we need to multiply entities without ncecessity. Commented May 12, 2016 at 0:27

1 Answer 1

2

Use jsawk:

curl -Ls http://github.com/micha/jsawk/raw/master/jsawk > jsawk

bash jsawk < /tmp/my_data.json 'return this.value.b1.amount'

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks George, vote up and mark your reply as answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.