LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-13-2024, 04:50 AM   #1
glennbtn
Member
 
Registered: Dec 2009
Posts: 154

Rep: Reputation: 19
Postgres query inside a json Field


Hi All

I need to get some data out of our phone system. All the calls are written to the database and the json field which contains the data I need.

Can anyone advise how I would do a db search in the json field for all the "digits_dialed": "2"

I can do the normal select * from v_xml_cdr for basic stuff but this has me stumped as there is so much data in this field in each column

Thanks
 
Old 01-13-2024, 01:23 PM   #2
nateb
LQ Newbie
 
Registered: Oct 2009
Distribution: PopOS, Alma
Posts: 12

Rep: Reputation: 2
Assuming the digits_dialed field is at the top level of your JSON column (and that your column is JSON-typed), you can use this to get the ID of all the fields where digits_dialed is the string '2'.

Code:
WITH some_table(id, json_field) AS (
  VALUES
    (1::integer, '{"digits_dialed": "2"}'::json),
    (2, '{"digits_dialed": "3"}'),
    (3, '{"digits_dialed": "2"}')
)
SELECT
  id,
  json_field->>'digits_dialed' as digits_dialed
FROM some_table
WHERE json_field->>'digits_dialed' = '2';
Fiddle.

If digits_dialed is nested in a JSON object, you'll need #>> with e.g. (json_field#>>'{path,to,digits_dialed}') (docs).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve the content of a json file using shell script .Without jq /json Historia Linux - Newbie 12 03-25-2020 09:50 AM
LXer: JSON Lines: record-style JSON LXer Syndicated Linux News 0 01-28-2020 08:03 PM
LXer: EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager LXer Syndicated Linux News 0 04-28-2016 03:41 AM
[SOLVED] Postgres 9.4 + unixODBC on Centos 6.5 problem connecting localhost postgres instance with ODBC isql rylan76 Linux - Server 1 07-22-2015 05:39 AM
Changes in postgres without restarting postgres venki Linux - General 3 07-19-2007 01:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration