11

I am trying to save an array of object in jsonb type in postgres

Entity

@Column({type: 'jsonb', array: true, nullable: true})
testJson: object[];

The json I am sending in postman

{
    
    "testJson": [
        {"skill": "docker", "experience": true},
        {"skill": "kubernetes", "experience": false}
    ]
}

I am getting error 'malformed array literal:'

Also kindly tell if I can query such data types?

2 Answers 2

18

I had the same problem. This worked for me

@Column('jsonb', {nullable: true})
testJson?: object[];
Sign up to request clarification or add additional context in comments.

Comments

5

Above answer is helpful. But it needs some update. Following code worked for me in nest.js.

interface dataType {
  key: number;
  asset: string;
  owner: string;
  value: string;
}

@Entity()
export class Will {
  @Column({
    type: "json",
    nullable: true,
    transformer: {
      to(value: dataType[]): string {
        return JSON.stringify(value);
      },
      from(value: string): dataType[] {
        return JSON.parse(value);
      },
    },
  })
  dataOutSideWill?: dataType[];
}

1 Comment

Quick question, why are you doing the transformation here, wouldn't that slow down the time it takes to fetch the data in terms of complex queries?

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.