Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is that possible to insert a whole json file ? #507

Open
aTewan opened this issue Apr 19, 2017 · 3 comments
Open

Is that possible to insert a whole json file ? #507

aTewan opened this issue Apr 19, 2017 · 3 comments

Comments

@aTewan
Copy link

aTewan commented Apr 19, 2017

Hello,
I was wondering if it was possible to insert a whole .json file

Cordially,

@victorleblais
Copy link

victorleblais commented May 2, 2017

Hello,

If your data inside your JSON is like this (array of items):

[
  {
    'name': 'Earth,
    'inhabited': true
  },
  {
    'name': 'Mars',
    'inhabited': false
  },
  // ...
]

then simply do:

// Load your JSON, here I'm just hardcoding it
var myJson = "[ { 'name': 'Earth, 'inhabited': true }, { 'name': 'Mars', 'inhabited': false }]"
var myData = JSON.parse(myJson)
// Insert into your database
db.insert(myData, function (err) {
  // you have 2 items in your db
})

@SheridanLGrant
Copy link

What if you don't want to hardcode it? What if it's in a huge file? I can't find the answer to this question anywhere...

@JamesMGreene
Copy link
Contributor

JamesMGreene commented Jul 19, 2018

That would be because it realistically has nothing to do with NeDB. You just get your data and then insert it. 🤷‍♂️

If you are using Node.js, for example, you could load the file with fs.readFileSync, fs.readFile, or fs.createReadStream and then JSON.parse the text, or even just require the JSON file (which will automatically parse it).

If you are operating in the browser, you would probably request the file via XMLHttpRequest or fetch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants