简体   繁体   中英

How to get particular key and value from json object to store in database?

{
    "purchaseorder": {
        "Meta": [{
            "InterchangeControlNumber": ["000004010"],
            "GroupControlNumber": ["833"],
            "DocumentControlNumber": ["5952"],
            "InterchangeSenderID": ["009483355"],
            "InterchangeReceiverID": ["076627454"],
            "GroupSenderID": ["009483355"],
            "GroupReceiverID": ["076627454"]
        }],
        "header": [{
            "address": [{
                "home": [{
                    "hno": ["134/123"],
                    "contact": ["898944343"],
                    "pin": ["272272"],
                    "district": ["telangana"]
                }],
                "office": [{
                    "hno": ["456/789"],
                    "contact": ["440838272"],
                    "pin": ["55833"],
                    "district": ["delhi"]
                }]
            }]
        }]
    }
}

I have JSON data like the above.

I want to store it in database as meta, home and office as table names, with the respective keys as column and value as cell data.

How do I the data from the JSON in node.js or JavaScript?

The JSON data can be any thing. I can pass the name of required table to be created, it should be automated.

I'm afraid I don't know too much about SQL Server, but hopefully this should get you going along the right lines on the Javascript side of things.

Convert JSON to Javascript Object

You can turn the JSON data into a key-value accessible object, and then access meta , home and office , as follows:

var data = JSON.parse(jsonData);

var meta = data.purchaseorder.Meta[0];
var home = data.purchaseorder.header[0].address[0].home[0];
var office = data.purchaseorder.header[0].address[0].office[0];

Then to pull out specific values you can do, for example:

var homeContact = home.contact; // Will return "898944343".

Retrieve Column Names

If, as you say, the data can change, one way that you could derive column names is this:

var metaColumns = Object.keys(meta);
var homeColumns = Object.keys(home);
var officeColumns = Object.keys(office);

Note: Javascript objects do not necessarily preserve property ordering, so these columns may not be in the same order as they appear in the data.

Create Table and Insert Data

You can put together corresponding arrays of column names and values for this purpose:

var allColumns = metaColumns + homeColumns + officeColumns;
var allValues = [];

for (let col of metaColumns) {
    allValues.push(meta[col]);
}

for (let col of homeColumns) {
    allValues.push(home[col]);
}

for (let col of officeColumns) {
    allValues.push(office[col]);
}

From here you can build your CREATE TABLE and INSERT SQL statements, with the column names and values as parameters, using a suitable node.js library for SQL Server, such as this .

Be aware that if this data comes from an untrusted source you should make sure to sanitise it properly to prevent the possibility of SQL injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM