1

Given a number of JSON document like this:

  {
    id: some_id,
    l1: {
      f1: [
        {
          c1: foo,
          c2: bar
        },
        {
          c1: foo1,
          c2: bar1
        }
      ],
      f2: [
        {
          c3: baz,
          c4: bar
        }
      ]   
    }
  }

How can I query MySQL 5.7 for f1....c1: foo1 -- ie lX is not given nor is the list position of the c1-c2 subdocument.

This is not a duplicate of Deep JSON query with partial path in PGSQL JSONB? since that is about PostgreSQL and this one is about MySQL.

1
  • You ask for this??:: SELECT JSON_EXTRACT(T.your_field, '$.l1.f1[1].c1'), JSON_EXTRACT(T.your_field, '$.l1.f1[2].c2'), ....; Full explited in MySQL docs, 11.6 The JSON Data Type Commented Feb 19, 2016 at 7:54

1 Answer 1

3
+50

This should do it:

SELECT JSON_CONTAINS(JSON_EXTRACT(Doc, '$.*.f1[*].c1'), '"foo1"') FROM table;

If you're using 5.7.9 or later, you can replace the JSON_EXTRACT function with the -> operator:

SELECT JSON_CONTAINS(Doc->'$.*.f1[*].c1', '"foo1"') FROM table;
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.