简体   繁体   中英

How to extract value from JSON with common_schema?

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)

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM