Cara menggunakan json functions in mysql


With PHP, you can connect to and manipulate databases.

MySQL is the most popular database system used with PHP.


What is MySQL?

  • MySQL is a database system used on the web
  • MySQL is a database system that runs on a server
  • MySQL is ideal for both small and large applications
  • MySQL is very fast, reliable, and easy to use
  • MySQL uses standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use
  • MySQL is developed, distributed, and supported by Oracle Corporation
  • MySQL is named after co-founder Monty Widenius's daughter: My

The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows.

Databases are useful for storing information categorically. A company may have a database with the following tables:

  • Employees
  • Products
  • Customers
  • Orders

PHP + MySQL Database System

  • PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

Database Queries

A query is a question or a request.

We can query a database for specific information and have a recordset returned.

Look at the following query (using standard SQL):

SELECT LastName FROM Employees

The query above selects all the data in the "LastName" column from the "Employees" table.

To learn more about SQL, please visit our SQL tutorial.


Download MySQL Database

If you don't have a PHP server with a MySQL Database, you can download it for free here: http://www.mysql.com


Facts About MySQL Database

MySQL is the de-facto standard database system for web sites with HUGE volumes of both data and end-users (like Facebook, Twitter, and Wikipedia).

Another great thing about MySQL is that it can be scaled down to support embedded database applications.

Look at http://www.mysql.com/customers/ for an overview of companies using MySQL.



MySQL 5.7.8+ supports native JSON type. While you have different ways to create json objects, you can access and read members in different ways, too.

Table of Contents

  • # Read JSON Array value
  • How extract JSON data from column in MySQL?
  • What is JSON extract () function in MySQL?
  • Can MySQL parse JSON?
  • How do I iterate through a JSON array in MySQL?

Main function is JSON_EXTRACT, hence -> and ->> operators are more friendly.

# Read JSON Array value

Create @myjson variable as JSON type (read more (opens new window)):

SELECT some members!

Extract path by -> or ->> Operators, while ->> is UNQUOTED value:

So col->>path is equal to JSON_UNQUOTE(JSON_EXTRACT(col,path)) :

As with ->, the ->> operator is always expanded in the output of EXPLAIN, as the following example demonstrates:

Read about inline path extract(+) (opens new window)

# Syntax

  • JSON_EXTRACT(json_doc,path[,...])
  • JSON_EXTRACT(json_doc,path)
  • JSON_EXTRACT(json_doc,path2,path2)

# Parameters

ParameterDescription
json_doc valid JSON document
path members path

Mentioned in MySQL 5.7 Reference Manual (opens new window)

  • Multiple matched values by path argument(s)

If it is possible that those arguments could return multiple values, the matched values are autowrapped as an array, in the order corresponding to the paths that produced them. Otherwise, the return value is the single matched value.

  • `NULL` Result when:
      - any argemunt is NULL - path not matched

      Returns NULL if any argument is NULL or no paths locate a value in the document.

  • My json is of the format ["a","b","c"]. I want to write a sql query such that

    select *
    from mytable
    where a_column in json_extract["a","b","c"]
    

    This logic does not work, because json_extract needs a path.

    Is there any work around for this in mysql 8.0

    asked May 17 at 5:34

    SELECT *
    FROM mytable
    WHERE JSON_CONTAINS('["a","b","c"]', JSON_QUOTE(a_column));
    

    I assume that the column a_column is a string-type column which contains a value like 'a'.

    answered May 17 at 12:50

    AkinaAkina

    17.5k2 gold badges11 silver badges19 bronze badges

    I have a problem extracting data in the JSON column. I'm new to this JSON method, unlike relation tables.

    Sample Table: every minute/second all websites activity will be saved in 1 column alongside timestamp.

    Expected Table: I wanted to produce is to extract the object details of a specific id, so I can have a full table of that objects.

    SELECT dtime, activity.id, activity.ssl, activity.online, activity.cert
    FROM logs
    WHERE activity.id = 3 
    

    dbfiddle:

    https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=7982cd1738b4fc594fb033403a492a6c

    asked Feb 2 at 19:01

    スペースタイムスペースタイム

    411 silver badge6 bronze badges

    1

    Finally, I found the problem.

    1. Make sure you check the version if JSON functions are supported.
    • MySQL8+
    • MariaDB 10.6.0+

    In my case, this was easily fixed by JSON_TABLE just like this post "read json array on mysql query"

    SELECT 
      dtime, 
      get_activity.* 
    FROM 
      logs, 
      JSON_TABLE(
        activity, 
        '$[*]' COLUMNS (
          `id` int(11) PATH '$.ID', 
          `ssl` int(1) PATH '$.SSL', 
          `online` int(1) PATH '$.Online', 
          `cert` text PATH '$.Cert'
        )
      ) get_activity 
    WHERE 
      get_activity.id = 3;
    

    All Result:

    Result using WHERE filter:

    dbfiddle

    answered Feb 3 at 18:10

    スペースタイムスペースタイム

    411 silver badge6 bronze badges

    How extract JSON data from column in MySQL?

    Key takeaway for extracting data from a JSON field in MySQL:.

    Use $. ... .

    Use $[index] to extract the value of an element from a JSON array..

    Use -> as a shortcut for JSON_EXTRACT if the value is not a string..

    What is JSON extract () function in MySQL?

    In MySQL, the JSON_EXTRACT() function returns data from a JSON document. The actual data returned is determined by the path you provide as an argument. You provide the JSON document as the first argument, followed by the path of the data to return.

    Can MySQL parse JSON?

    In MySQL, JSON values are written as strings. MySQL parses any string used in a context that requires a JSON value, and produces an error if it is not valid as JSON.

    How do I iterate through a JSON array in MySQL?

    SET products = (SELECT JSON_EXTRACT(dataObject, "$. products")); SET productId = (SELECT JSON_EXTRACT(products, "$[0]. productId")); I get the inner products json and the id of the 0th product.