I am attempting to write a system which configures itself based on a set of JSON configuration files. I have some configuration options which are specific to certain processes but some which are common to all. Is there a way for me to include a common JSON configuration file into more specific ones. ex:
Common.json
# Common example
{
"log-level" : "debug",
"version" : "1.21"
}
Specific.json
# Specific example
# include <common.json> ????
{
"config" : {
"process" : "distro-center"
"id" : "12-3"
"log-path" : "/var/log/blah"
}
}
So the resulting JSON would effectively be
{
"log-level" : "debug",
"version" : "1.21"
"config" : {
"process" : "distro-center"
"id" : "12-3"
"log-path" : "/var/log/blah"
}
}
Ideas?
Related
In my database class we were given an assignment to work with two JSON files (add them to a mongodb atlas collection and query certain results)
Both JSON files had "errors" the first being :
{ "_id" : { "$oid" : "50b59cd75bed76f46522c34e" }, "student_id" : 0, "class_id" : 2, "scores" : [ { "type" : "exam", "score" : 57.92947112575566 }, { "type" : "quiz", "score" : 21.24542588206755 }, { "type" : "homework", "score" : 68.19567810587429 }, { "type" : "homework", "score" : 67.95019716560351 }, { "type" : "homework", "score" : 18.81037253352722 } ] }
and the second being :
{"_id":0,"name":"aimee Zank","scores":[{"score":1.463179736705023,"type":"exam"},{"score":11.78273309957772,"type":"quiz"},{"score":35.8740349954354,"type":"homework"}]},
{"_id":1,"name":"Aurelia Menendez","scores":[{"score":60.06045071030959,"type":"exam"},{"score":52.79790691903873,"type":"quiz"},{"score":71.76133439165544,"type":"homework"}]},
I fixed error 1 by removing the $oid and replacing it with just oid: as there was an error trying to add objects with $oid as a value to my collection. I also needed to add everything to an array.
I fixed the second by putting the entire object inside an array [].
When I asked my professor why these errors were in the JSON files and if it was on purpose, he said that they were there on for a reason and that we needed to find a "work around".
I am curious what work around there is to load JSON data that is incorrect into a collection? I am at a complete loss as to what he expected. Is there some way I can just load individual objects line by line from the JSON file to the collection?
This is how I loaded the JSON data after fixing the files directly:
const fs = require('fs');
var data = JSON.parse(fs.readFileSync("./students.json"));
JSON.stringify(data);
const database = "college";
const collection = "students";
use(database);
db.students.drop();
db.createCollection(collection);
db.students.insertMany(data);
--- All the importing of data should be done in VS Code and not using --mongodb import
And a side note that this assignment has since passed so I am not asking for help in completing my homework, simply trying to see if there was something I could of done that would not of required me to edit the JSON file itself. My professor has not responded to me regarding this question.
A JSON string string passes the jsonlint test.
response = [
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"7461221587662919569"
],
}
},
"text" : "where they would 'transfer to' next.",
"lang" : "en",
}
},
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"6613144915874808065"
],
}
},
"text" : "produto regional.",
"lang" : "pt"
}
}
]
However, after processing
require 'json'
file = File.read('/Users/main/jugg//article_samples.js')
data_hash = JSON.parse(file)
One is left with an array, whereas more frequently a hash with a name labels a subsequent array, where one works with that nomenclature such as response['data']
But in this case the array is not accessible via response[0]. How can this be considered as an array in order to process each individual element collection.each do |member|?
A curiosity: data_hash.class => NilClass
The response = ... code from article_samples.js is JavaScript, not JSON. This initializes a variable named response with a JavaScript array.
To use this as JSON, then rename the file to article_samples.json and remove response = from the file. The first line should start with [.
Now your second block of code should work just fine as long as the article_samples.json file is in the correct path.
On a side note, I suggest that you find a way to make the path more flexible. The way you have it currently hard coded is tied directly to your current machine's file system. This won't work if you want to run this code from another machine because the folder /Users/main/jugg probalby won't exist.
If this is a web server with ruby on rails, then one solution is to create an environment variable with the path where this file is stored.
I have been given a text file, containing thousands of json documents (not ideal I know).
I need to put said documents into a mongodb collection.
So far, I have saved the text file as JSON and tried to mongoimport, added commas between each document and attempted mongorestore with a bson equivalent - all to no success
Here is an example of what is in the text file:
{
"_id" : ObjectId("78ahgodjaodj90231"),
"date" : ISODate("1970-01-01T00:00:00+0000),
"comment" : "Hello"
}
{
"_id" : ObjectId("99151gdsgag5464ah"),
"date" : ISODate("1970-01-02T00:00:00+0000),
"comment" : "World"
}
and so on...
Using mongoimport I get this error message:
Failed: invalid JSON input. Position: 16. Character: O
After saving as a BSON file, using mongorestore I also get this error:
Failed: db.collection: error restoring from file.bson: reading bson input: invalid BSONSize: 537534587 bytes
Any help would be greatly appreciated!
Let's say we have the following data in the file:
{
"_id" : ObjectId("78ahgodjaodj90231"),
"date" : ISODate("1970-01-01T00:00:00+0000),
"comment" : "Hello"
}
{
"_id" : ObjectId("99151gdsgag5464ah"),
"date" : ISODate("1970-01-02T00:00:00+0000),
"comment" : "World"
}
We need to refactor it to a code like below and save it with .js extension, say insert_data.js
db.collection.insertMany([
{
"_id" : ObjectId("78ahgodjaodj90231"),
"date" : ISODate("1970-01-01T00:00:00+0000),
"comment" : "Hello"
},
{
"_id" : ObjectId("99151gdsgag5464ah"),
"date" : ISODate("1970-01-02T00:00:00+0000),
"comment" : "World"
}
])
Finally run the following command:
mongo HOST:PORT/DB insert_data.js
I managed to import the documents successfully using Studio3T's import feature.
After renaming the text file to a JSON file, and letting Studio 3T validate the JSON before import, it worked perfectly.
Not the best solution, but it seemed to work for me.
I am currently trying write a JSON Schema to validate a catalogue in .json file format.
It contains information about some of the possible components of an assembly and then several entries for actual assemblies, which consists of strings referencing those components (via the key in the .json) file.
The .json file looks something like this:
{
"sensors" : {
"sensor_type_temperature_1" : { ... info about first temperature sensor ... },
"sensor_type_tempearature_2" : { ... info about second temperature sensor ... },
"sensor_type_humidity_1" : { ... info about first humidity sensor ... }, ...
}, ...
"masts" : {
"mast_type_1" : { ... info about first mast type ... },
"mast_type_2" : { ... info about second mast type ... }, ...
}, ...
"observation_stations" : {
"first_station" : {
"sensors" : ["sensor_id_tempearature_2", "sensor_id_humidity_1"],
"mast" : "mast_type_2",
"location" : { ... location ...}, ...
"second_station" : {
"sensors" : ["sensor_id_tempearature_1"],
"mast" : "mast_type_1",
"location" : { ... location ...}, ...
}
}
}
What I want to do now is to use a JSON Schema to validate my file. I am fine for the data formats and I am familiar with using enums for restricting the actual content of e.g. contents of the "mast" or "sensors".
How can I use the scheme to verify that all entries in of "sensors" and "mast" in an observation station are also keys in the "sensors" and "masts" section of the same .json file?
Update
After the answer I got to use fileUID: true, here is how to implement the dynamic key with AngularFire2 and typescript. It could be useful to some people:
this.af.database.object(`users/${userID}/submitted-files/`).update({
[nextFileKey]: true
});
Do I need to create a back-reference to userUUID in the files?
Original question
Here is my current database structure. This question applies in my case at Firebase, but any JSON database would look the same.
{
"users" : {
"0gIo3Ak2EFbLDSSCMSEpciuWnB83" : {
"email" : "username#example.com",
"hasAcceptedTerms" : true,
"state" : {
"isProfileComplete" : true
},
"submitted-files": { // FILES REFERENCES WILL GO THERE }
}
}
}
I want my user to be able to upload some files, which will be reviewed then approved by an external watcher script. On file upload, I'd like to create a new files area in my Firebase database, to obtain this structure:
{
"files" : {
"-K_jG4usPu_4yLED7j8L" : {
"fileUUID" : "448f4sd8f4sd4fsdf494ds84f98sd4f",
"filename" : "document.pdf"
"user" : "0gIo3Ak2EFbLDSSCMSEpciuWnB83"
}
},
"users" : {
"0gIo3Ak2EFbLDSSCMSEpciuWnB83" : {
"email" : "username#example.com",
"hasAcceptedTerms" : true,
"state" : {
"isProfileComplete" : true
},
"submitted-files": { // FILES REFERENCES WILL GO THERE }
}
}
}
The problem is to link now the file UUID in the user/submitted-files path.
In perfect world I'd add this to the user data:
"submitted-files" : {
"-K_jG4usPu_4yLED7j8L",
"SecondFileUUID"
"ThirdFileUUID"
}
But since I must enter a key/value pair as it's JSON Database, how can I efficiently, in my users part list all submitted files UUID without any other non-required information?
Do I have to replicate the whole file information in the user path to obtain:
"users" : {
"0gIo3DQ9EFbLDSSCMSEpciuWnB83" : {
...
"submitted-files" : {
"-K_jG4usPu_4yLED7j8L" : {
"fileUUID" : "448f4sd8f4sd4fsdf494ds84f98sd4f",
"filename" : "document.pdf"
"user" : "0gIo3Ak2EFbLDSSCMSEpciuWnB83"
}
}
}
}
or is there any way to only enter the reference to file UUID in the user/submitted-files path?
I want advices on structuring my database in a correct "JSON way" good-practices.
You were pretty close with:
"submitted-files" : {
"-K_jG4usPu_4yLED7j8L",
"SecondFileUUID"
"ThirdFileUUID"
}
Just assign them a value of true instead:
"submitted-files" : {
"-K_jG4usPu_4yLED7j8L": true,
"SecondFileUUID": true,
"ThirdFileUUID": true
}
They explain this in a few of their tutorials.