{"id":24535,"date":"2022-03-23T09:15:14","date_gmt":"2022-03-22T23:15:14","guid":{"rendered":"https:\/\/database.guide\/?p=24535"},"modified":"2022-04-02T09:29:12","modified_gmt":"2022-04-01T23:29:12","slug":"sqlite-json_group_object%ef%bf%bc","status":"publish","type":"post","link":"https:\/\/database.guide\/sqlite-json_group_object%ef%bf%bc\/","title":{"rendered":"SQLite JSON_GROUP_OBJECT()\ufffc"},"content":{"rendered":"\n<p>The SQLite <code>json_group_object()<\/code> function is an aggregate function that returns a JSON object comprised of all name\/value pairs in the aggregation.<\/p>\n\n\n\n<p>In other words, it constructs a JSON object from the values provided by its arguments.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>json_group_object(NAME,VALUE)<\/code><\/pre>\n\n\n\n<p>Where <code>NAME, VALUE<\/code> represents the name\/value pairs to be used in the resulting JSON object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we have the following table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT PetId, PetName \nFROM Pets;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------+---------+\n| PetId | PetName |\n+-------+---------+\n| 1     | Homer   |\n| 2     | Yelp    |\n| 3     | Fluff   |\n| 4     | Brush   |\n+-------+---------+<\/pre>\n\n\n\n<p>We can use <code>json_group_object()<\/code> to output that result as a JSON document that contains name\/value pairs based on the columns in the table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT json_group_object(PetId, PetName)\nFROM Pets;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------------------------------+\n|        json_group_object(PetId, PetName)         |\n+--------------------------------------------------+\n| {\"1\":\"Homer\",\"2\":\"Yelp\",\"3\":\"Fluff\",\"4\":\"Brush\"} |\n+--------------------------------------------------+<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The SQLite json_group_object() function is an aggregate function that returns a JSON object comprised of all name\/value pairs in the aggregation. In other words, it constructs a JSON object from the values provided by its arguments.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[73,113,93,80,20],"class_list":["post-24535","post","type-post","status-publish","format-standard","hentry","category-sqlite","tag-aggregate-functions","tag-aggregation","tag-functions","tag-json","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/24535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=24535"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/24535\/revisions"}],"predecessor-version":[{"id":24737,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/24535\/revisions\/24737"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=24535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=24535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=24535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}