0

I have a JSON string (in MySQL) and I want to get the name where id=2:

  SET @json := '  
  {
     "list": [
        {"id": "1", "name": "John"},
        {"id": "2", "name": "Ben"},
        {"id": "3", "name": "Clara"}
      ] 
  }           
';
SELECT common_schema.extract_json_value(@json, '/list/name') ;

This way I get 'John Ben Clara', that is all 3 names. How can I get the one with id=2?

Among others, I tried:

SELECT common_schema.extract_json_value(@json, '/list/descendant-or-self::name[2]') ;

with no success.

(I can't use MySQL 5.7, I have to use an older version, that's why I use common schema)

P.G
  • 11
  • 2

1 Answers1

0

Try this:

SELECT common_schema.extract_json_value(@json, 'descendant-or-self::list[2]/name') ;