1

I have some JSON along the following lines, the format of which cannot, unfortunately, be changed:

{ 
  "elements": {
    "nodes": [
      {
        "data": {
          "name": "Name here",
          "color": "#FFFFFF",
          "id": "n0"
        }
      }
    ]
  }
}

This is stored in a postgres database and I'd like to pull out records by means of the id embedded in the JSON above. So far I've tried stuff like this:

SELECT "data".* FROM "data" WHERE payload #>> '{elements,nodes,data,id}' = 'n0';

...without success; although this query runs it returns nothing. Can anyone suggest how this might be done?

1 Answer 1

1

Create schema:

create table json (
    id serial primary key,
    data jsonb
);

insert into json (data) values (
'{ "elements": {
    "nodes": [
      {
        "data": {
          "name": "Name here",
          "color": "#FFFFFF",
          "id": "n0"
        }
      }
    ]
  }
}
'::jsonb);

Query itself:

select * from json js,jsonb_array_elements(data->'elements'->'nodes') as node 
 where node->'data'->>'id' = 'n0';
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.