如何使用 Node.js 将特定键:Json 的值插入到 postgres table 中

How do I insert a specific key: value of Json into a postgres table using Node.js

我有一个 json 变量,我在其中获取了所有数据。我想使用一个特定的键及其值将其插入到我的 table.

的特定列中

这是一个例子:

{
    'Code bati': 'Pas',
    Etage: '0',
    Zone: 'FST',
    'Pièce': '000',
    'Priorité': 5,
    'Désignation': 'Salle de cours',
    'Type d’installation': '',
    'Date de mise en service': '',
    "Date d'installation": '',
    Maintenance: '',
    'Entreprise ayant le contrat de maintenan': '',
    'Date de visite': 44525,
    Marque: 'Epson',
    'Modèle': 'EB-1980WU',
    'N° série': 'V5YF630231L',
    'Heure Lampe': 1796,
    Position: 'centré',
    HDMI: 'oui',
    VGA: 'oui',
    Video: 'non',
    Automate: 'MLC ???',
    Liaison: 'RS232',
    'Type écran': 'manuel',
    'Cage sécurité': 'oui',
    'Statut HDMI': 'à tester',
    'Statut VGA': 'à tester'
  },

这是我的数组,我想以键 'Zone' 及其属性 'FST' 为例,将它们插入到我的 postgres 数据库的特定 table 中。 你有解决方案吗?

我有点不清楚你是否已经为节点安装了PG lib。我过去用过这个(当我需要简单快速的东西时): https://www.npmjs.com/package/pg.

无论如何,假设你在一个变量中有这个 JSON,然后你想把它解析成一个对象,然后你可以挑选你想要的任何东西,然后插入到数据库中。

顺便说一句,你的 JSON 格式不正确,它已经被格式化为一个对象......在下面的你的JSON var中我已经为你正确地格式化了它(通过采取你有,并用 JSON.stringify(whatYouHad))

包装它
const yourJSON = '{"Code bati":"Pas","Etage":"0","Zone":"FST","Pièce":"000","Priorité":5,"Désignation":"Salle de cours","Type d’installation":"","Date de mise en service":"","Date d'installation":"","Maintenance":"","Entreprise ayant le contrat de maintenan":"","Date de visite":44525,"Marque":"Epson","Modèle":"EB-1980WU","N° série":"V5YF630231L","Heure Lampe":1796,"Position":"centré","HDMI":"oui","VGA":"oui","Video":"non","Automate":"MLC ???","Liaison":"RS232","Type écran":"manuel","Cage sécurité":"oui","Statut HDMI":"à tester","Statut VGA":"à tester"}';

const parsedJSON = JSON.parse(yourJSON);

const query = 'INSERT INTO your_table (zone) VALUES ();
const bindings = [parsedJSON.Zone];
const insertResult = await dbClient.query(query, bindings);
console.log(insertResult);

否则你需要再澄清一些...