0

This is my JSON value

"order": {
        "items": [{
            "amount": 30000,
            "product_name": "product A",
            "quantity": 3,
         }, {
            "amount": 1000,
            "product_name": "product B",
            "quantity": 1,
        }],
        "currency": "USD"
    }

I want get values of amount from the array using mysql queries. I tried select json_extract_path_text('order','items') but it fetches whole value

 [{
            "amount": 30000,
            "product_name": "product A",
            "quantity": 3,
         }, {
            "amount": 1000,
            "product_name": "product B",
            "quantity": 1,
        }]

Can you help me?

3
  • 2
    This is why you don’t store JSON in database tables. Commented Dec 5, 2013 at 4:45
  • Does MySQL support querying JSON? Didn't you mean PostreSQL? Commented Dec 5, 2013 at 5:10
  • @ Mosty Mostacho Thanks for notify this. I made a great mistake. You are right Its postgresql Commented Dec 5, 2013 at 5:44

1 Answer 1

1

You can use json_array_elements() function:

select
    (value->>'amount')::int as amount
from json_array_elements(
    '{"order":
         {  "items": [
                {"amount": 30000,"product_name": "product A", "quantity": 3},
                {"amount": 1000,"product_name": "product B","quantity": 1}
            ],
            "currency": "USD"}
     }'::json->'order'->'items'
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.