0

I have the following JSON object in PostgreSQL. I am trying to write a query to get me the details of "Elements"

{
  "ItineraryId": 0,
  "ForceSingleSearchForFlights": null,
  "ExcludeBasicFareBrand": null,
  "UserNodes": [
    {
      "User": {
        "Id": "-2",
        "GivenName": "Wiliam Never",
        "PreferredAirport": "",
        "TravelerGroupId": null,
        "ExternalId": null,
        "ExternalId2": null,
        "CheckCCFieldForTraveler": false,
        "NotificationEmailOverride": null,
        "PaxType": 0,
        "Title": "Dr",
        "MiddleName": null,
        "DateOfBirth": "1980-01-15T00:00:00Z",
        "PhoneNumber": null,
        "TsaInfo": null,
        "Source": "RequestEndPoint",
        "AllowBookingGuestTraveler": null,
        "GDSTravelerProfileContainsFirstName": false,
        "ProfilesFound": {
          "Agency": null,
          "Corporate": null,
          "Traveler": null
        },
        "UserId": "-2",
        "FirstName": "Wiliam",
        "LastName": "Never"
      },
      "Elements": null
    }
  ]
}

I have tried with multiple query options like

select itineraryintent -> 'UserNodes' ->> 'Elements' AS intent from Itinerary";

but i am always getting the following error

ERROR:  operator does not exist: text -> unknown
LINE 1: select itineraryintent -> 'UserNodes' AS intent from "Worksp...
                               ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts. 

SQL state: 42883
Character: 24
2
  • 1
    From the error it looks like itineraryintent is a string type not a json type. Add the table definition, as text, to your question text. Commented Dec 26, 2023 at 16:53
  • select ((itineraryintent::jsonb -> 'UserNodes')->0)->>'Elements' AS intent from "Itinerary"; Commented Dec 26, 2023 at 16:57

1 Answer 1

1

Use json_array_elements like the following

SELECT 
    elements.*
FROM 
    Itinerary,
    json_array_elements(itineraryintent -> 'UserNodes') AS elements
WHERE 
    elements -> 'User' ->> 'Elements' IS NOT NULL;
Sign up to request clarification or add additional context in comments.

Comments

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.