Here is my situation, i am importing from multiple data sources into my json backend.
The mapping file will look something like this sample below. So the question is how could i go about it to effectively look up the mapping on an import from this json file and use it when to import the data into my backend. So the concept is to pass 2 objects to my function, the mapping file and the json object which holds the data from the vendor. Then i have to use the mapping and loop thru data to create an object / doc and insert in my backend with correct mappings.
{
"vendor" : "ABCVendor",
"version" : "2.01",
"mappings" : [
{ "fieldName" : "fName",
"dbName" : "FirstName",
"required" : true,
"type" : "string"
},
{ "fieldName" : "mName",
"dbName" : "MiddleName",
"required" : false,
"type" : "string"
},
{ "fieldName" : "lName",
"dbName" : "LastName",
"required" : true,
"type" : "string"
}
]
}
Here is what the input would look like
{
"fname" : "Steve",
"mname" : "T",
"lname" : "Miller"
}
Ad here is what i would like to get out
{
"FirstName" : "Steve",
"MiddleName" : "T",
"LastName" : "Miller"
}
You could do something like this, if the insert is always the same.
this may not be the most performant way to do it though.
"use strict";
const fileConfig = {
"vendor": "ABCVendor",
"version": "2.01",
"mappings": [
{
"fieldName": "fName",
"dbName": "FirstName",
"required": true,
"type": "string"
},
{
"fieldName": "mName",
"dbName": "MiddleName",
"required": false,
"type": "string"
},
{
"fieldName": "lName",
"dbName": "LastName",
"required": true,
"type": "string"
}
]
};
const funcInput = {
"fName": "Steve",
"mName": "T",
"lName": "Miller"
};
function change(config, input) {
const result = {};
config.mappings.forEach((fe) => {
result[fe.dbName] = input[fe.fieldName];
});
return result;
}
console.log(change(fileConfig, funcInput))
You can do this all with reduce (although there does appear to be a casing difference in your mapping vs your object which ive used toLowerCase to get aeround - maybe uunncessary if that was just a typo on your part)
const config = {
"vendor" : "ABCVendor",
"version" : "2.01",
"mappings" : [
{ "fieldName" : "fName",
"dbName" : "FirstName",
"required" : true,
"type" : "string"
},
{ "fieldName" : "mName",
"dbName" : "MiddleName",
"required" : false,
"type" : "string"
},
{ "fieldName" : "lName",
"dbName" : "LastName",
"required" : true,
"type" : "string"
}
]
}
const input = {
"fname" : "Steve",
"mname" : "T",
"lname" : "Miller"
}
const result = config.mappings.reduce(
(acc, {fieldName, dbName}) => ({...acc, [dbName]: input[fieldName.toLowerCase()]})
,{})
console.log(result)
Related
Suppose that I have a collection with documents like below
{
"location" : "Tokyo",
"region" : "Asia",
"attraction": {
"transportation" : "Subway",
"food" : {
"food_0" : {
"name" : "Sushi",
"price" : 100,
"restaurant" : "Ookinza"
},
"food_1" : {
"name" : "Sashimi",
"price" : 200,
"restaurant" : "Hibiki"
},
"food_2" : {
"name" : "N/A",
"price" : "N/A",
"restaurant" : "N/A"
}
}
}
},
{
"location" : "Taipei",
"region" : "Asia",
"attraction": {
"transportation" : "Subway",
"food" : {
"food_0" : {
"name" : "Bubble tea",
"price" : 50,
"restaurant" : "The Alley"
},
"food_1" : {
"name" : "Oyster cake",
"price" : 100,
"restaurant" : "Night market"
},
"food_2" : {
"name" : "N/A",
"price" : "N/A",
"restaurant" : "N/A"
}
}
}
},
{
"location" : "Toronto",
"region" : "North America",
"attraction": {
"transportation" : "Uber",
"food" : {
"food_0" : {
"name" : "Raman",
"price" : 300,
"restaurant" : "Kinto"
},
"food_1" : {
"name" : "Bubble tea",
"price" : 200,
"restaurant" : "Fresh Fruit"
},
"food_2" : {
"name" : "N/A",
"price" : "N/A",
"restaurant" : "N/A"
}
}
}
},
How do I find documents that have matching field in the child object of Food?
i.e. If I want to find document that has restaurant:"Fresh Tea"?
Currently what I have:
app.get(route, (req, res) => {
var detail = {};
if(req.query.location){
detail['location'] = req.query.location.toUpperCase();
}
if(req.query.region){
detail['region'] = req.query.region.toUpperCase();
}
if(req.query.transportation){
detail['attraction.transportation'] = new RegExp(req.query.transportation.split(","), "i"),
}
if(req.query.restaurant){
detail['attraction.food.food_0'] = req.query.restaurant;
}
db.collection(config.dbCollections.foodDB)
.aggregate([
$match: detail,
},
{
$lookup: {
... // code continues
Right now detail['attraction.food.food_0'] = req.query.restaurant is only able to find document that has matching food_0.restaurant, but I still can't find a way to make it check all child objects within "food".
Updated with more info:
User has the option to enter multiple search categories, and I want to combine all the search requests into "detail" and find all matching results. I.e. If user looks for transportation="Subway" and food="Bubble tea", then both Taipei and Toronto should come up as result.
Using dynamic value as field name is generally considered as anti-pattern and should be avoided. Nevertheless, you can convert the object attraction.food to an array of k-v tuple and perform the search with your criteria. For your case, $anyElementTrue with $map will help with processing the array.
db.collection.aggregate([
{
"$addFields": {
"test": {
"$anyElementTrue": {
"$map": {
"input": {
"$objectToArray": "$attraction.food"
},
"as": "t",
"in": {
$or: [
{
$eq: [
"$$t.v.transportation",
"Subway"
]
},
{
$eq: [
"$$t.v.name",
"Bubble tea"
]
}
]
}
}
}
}
}
},
{
$match: {
test: true
}
},
{
"$unset": "test"
}
])
Here is the Mongo Playground for your reference.
A possible aggregation pipeline
Add a temporary field using $addFields and $objectToArray which does something similar to javascript Object.entries()
Do the matching
Remove the added temporary field using $project 0
playground
db.collection.aggregate([
{
"$addFields": {
"foodArray": {
"$objectToArray": "$attraction.food"
},
},
},
{
"$match": {
"foodArray.v.restaurant": "Fresh Fruit"
}
},
{
"$project": {
"foodArray": 0
},
},
])
I have the following sample JSON object:
var data = [ {
"id" : 1,
"name" : "Abc",
"age" : 30,
"married" : true,
"city": "ABC"
}, {
"id" : 2,
"name" : "Def",
"age" : 25,
"married" : true,
"city": "ABC"
}, {
"id" : 3,
"name" : "Pqr",
"age" : 28,
"married" : false,
"city": "ABC"
}, {
"id" : 4,
"name" : "Xyz",
"age" : 40,
"married" : true,
"city": "ABC"
} ];
I want to return true and store it in a variable if all city key values are ABC only, or else it should return false(i.e if one of city key values is not ABC) from the given JSON object. Can anyone please let me know on how to achieve this. Thanks in advance.
Using Array#every:
const data = [ { "id" : 1, "name" : "Abc", "age" : 30, "married" : true, "city": "ABC" }, { "id" : 2, "name" : "Def", "age" : 25, "married" : true, "city": "ABC" }, { "id" : 3, "name" : "Pqr", "age" : 28, "married" : false, "city": "ABC" }, { "id" : 4, "name" : "Xyz", "age" : 40, "married" : true, "city": "ABC" } ];
const valid = data.every(({ city }) => city === 'ABC');
console.log(valid);
Three possible ways of achieving this:
Using Array#every
Using Array#some
Using Array#filter
let data = [{id:1,name:"Abc",age:30,married:!0,city:"ABC"},{id:2,name:"Def",age:25,married:!0,city:"ABC"},{id:3,name:"Pqr",age:28,married:!1,city:"ABC"},{id:4,name:"Xyz",age:40,married:!0,city:"ABC"}];
console.log(data.every(({ city }) => city === "ABC"));
console.log(!data.some(({ city }) => city !== "ABC"));
console.log(!data.filter(({ city }) => city !== "ABC").length);
simply :
data.filter(x => x.city === 'ABC')
Please do a more detailed search about the problem before opening a topic.
data.some((el) => el.city !== "ABC")
I am iterating data from a JSON file that I previousy fetch with the following code:
getData().then(data => {
for (const key in data) {
if (data.hasOwnProperty(key)) {
// do something with the data
}
}
}
The json file is very long, but it look something like this:
{
"01": {
"id" : "01",
"title" : "example1",
"size" : "100",
"pictures" : []
},
"02": {
"id" : "02",
"title" : "example2",
"size" : "0",
"pictures" : []
},
"03": {
"id" : "03",
"title" : "example3",
"size" : "300",
"pictures" : [
{ "pic_name1" : "example_pic1", "source" : "http://example.pic/1234" },
{ "pic_name2" : "example_pic2", "source" : "http://example.pic/4321" },
]
},
}
Now, to create a function that will filter through my data I need to put all of the size in a separate array (that I will later work with) and I tried this (inside the IF condition)
let sizes = new Array(data[key].size);
What I need to return is an array, but I get instead a list of array for each size:
["100"]["0"]["300"]...
How do I return a single array with all sizes as a list?
Simply map over each value and pluck the size. E.g.
const data = {
"01": {
"id" : "01",
"title" : "example1",
"size" : "100",
"pictures" : []
},
"02": {
"id" : "02",
"title" : "example2",
"size" : "0",
"pictures" : []
},
"03": {
"id" : "03",
"title" : "example3",
"size" : "300",
"pictures" : [
{ "pic_name1" : "example_pic1", "source" : "http://example.pic/1234" },
{ "pic_name2" : "example_pic2", "source" : "http://example.pic/4321" },
]
},
};
const sizes = Object.values(data).map(({size}) => size);
console.log(sizes);
Using ES8 new features inside the if condition,
let sizes = Object.values(data).map(({size}) => size);
This question already has answers here:
Loading local JSON file
(26 answers)
Closed 7 years ago.
I have a JSON file with this as the content:
{
"residents": [
{
"name" : "Jacob",
"title" : "King",
"gender" : "Male",
},
{
"name" : "Luthor",
"title" : "Prince",
"gender" : "Male"
},
{
"name" : "Mileena",
"title" : "Princess",
"gender" : "Female"
},
]
}
I want to read the json in JavaScript, but certainly, I have no idea. How would I come about with this problem?
You can use JQuery for this.
$.ajax({
url: "\data.json", //name of your json file
success: function (data) {
var obj = JSON.parse(data);
}
});
Then you can get necessary property by the call of:
obj.residents[0].name
and so on.
It depends on environment that you use.
If you work with node.js you should read this file with API - fileRead
Then you should use JSON.parse for parsing it in {Object}
If you work in browser and some server has path to static with this file you can use ajax for getting this file
In both cases you should do such steps:
Get file as {String}
Parse to {Object}
Assuming this is for web because of your web tag, the easiest method is using jquery.
$.get('http://ip.jsontest.com', function(data) { console.log(data); });
If the server uses the correct MIME type in the response of the JSON then jquery will convert it to an object and "data" in the above example will be the evaluated JSON.
If the server does not use the correct MIME type you can wrap it in JSON.parse:
var json = JSON.parse(data);
Something like this?
$(function () {
$("#btnTest").click(function () {
var data = {
"residents": [{
"name": "Jacob",
"title": "King",
"gender": "Male",
}, {
"name": "Luthor",
"title": "Prince",
"gender": "Male"
}, {
"name": "Mileena",
"title": "Princess",
"gender": "Female"
}, ]
};
//If you're getting it somewhere from ajax call, than possibly it would be in string , in that case you need to `parse` it as data = JSON.parse(data);
$.each(data.residents, function (index, value) {
$("#data").append(value.name + " " + value.title + " " + value.gender + " </br>");
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="btnTest" value="Try Me!" />
<div id="data">
</div>
Try this..
var mymessage='{
"residents": [
{
"name" : "Jacob",
"title" : "King",
"gender" : "Male",
},
{
"name" : "Luthor",
"title" : "Prince",
"gender" : "Male"
},
{
"name" : "Mileena",
"title" : "Princess",
"gender" : "Female"
},
]
}';
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.residents.length; i++) {
var resident= jsonData.residents[i];
console.log(resident.name);
}
Using javascript you would just do...
obj = JSON.parse({
"residents": [
{
"name" : "Jacob",
"title" : "King",
"gender" : "Male",
},
{
"name" : "Luthor",
"title" : "Prince",
"gender" : "Male"
},
{
"name" : "Mileena",
"title" : "Princess",
"gender" : "Female"
},
]
});
You now have the JSON in an object that you can manage
console.log(obj);
You can fetch it like below
var text = '{
"residents":[
{
"name" : "Jacob",
"title" : "King",
"gender" : "Male",
},
{
"name" : "Luthor",
"title" : "Prince",
"gender" : "Male"
},
{
"name" : "Mileena",
"title" : "Princess",
"gender" : "Female"
},
]
}'; // That is your data from request
var obj = JSON.parse(text);
alert(obj.residents);
alert(obj.residents[0].name);
<script>
json_text = '{
"residents":[
{
"name" : "Jacob",
"title" : "King",
"gender" : "Male",
},
{
"name" : "Luthor",
"title" : "Prince",
"gender" : "Male"
},
{
"name" : "Mileena",
"title" : "Princess",
"gender" : "Female"
},
]}';
var obj = JSON.parse(json_text);
alert(obj.residents[2].name);
</script>
This code will bring up an alert dialog box in the browser with the value of name attribute of the second object in the array residents.
First and foremost your json string has error.
{
"residents": [
{
"name" : "Jacob",
"title" : "King",
"gender" : "Male",
},
{
"name" : "Luthor",
"title" : "Prince",
"gender" : "Male"
},
{
"name" : "Mileena",
"title" : "Princess",
"gender" : "Female"
},
]
}
There won't be comma after the third parenthesis from the end.
JSONString = '{ . . . . }';
JSONObject = JSON.parse(JSONString);
This way you can access the json data from JSONObject.
try this
<!docTpye html>
<html>
<head>
<script>
var data={
"residents": [
{
"name" : "Jacob",
"title" : "King",
"gender" : "Male",
},
{
"name" : "Luthor",
"title" : "Prince",
"gender" : "Male"
},
{
"name" : "Mileena",
"title" : "Princess",
"gender" : "Female"
},
]
}
for(var i=0;i<data.residents.length;i++){
console.log(data.residents[i].name);
alert(data.residents[i].name);
}
</script>
</head>
<body>
</body>
</html>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
{
"action" : "get",
"application" : "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
"params" : {
"ql" : [ "select * where username='pqr'" ]
},
"path" : "/logs",
"uri" : "https://api.usergrid.com/serv-d/demo1/logs",
"entities" : [ {
"uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
"type" : "log",
"created" : 1425036869840,
"modified" : 1425036869840,
"metadata" : {
"path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
},
"password" : "pqr",
"username" : "pqr"
}],
"timestamp" : 1425359738746,
"duration" : 15,
"organization" : "serv-d",
"applicationName" : "demo1",
"count" : 1
}
This is the server side response to my app and I want to get the user name and password values only.
Use JSON.parse() to parse the JSON response into a Javascript object. Then you can access the properties.
var obj = JSON.parse(response);
The username and password are in an array that's in the entities property, so you can access them as:
var username = obj.entities[0].username;
var password = obj.entities[0].password;
Get the data in a variable, say in variable data.
var data = {
"action" : "get",
"application" : "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
"params" : {
"ql" : [ "select * where username='pqr'" ]
},
"path" : "/logs",
"uri" : "https://api.usergrid.com/serv-d/demo1/logs",
"entities" : [ {
"uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
"type" : "log",
"created" : 1425036869840,
"modified" : 1425036869840,
"metadata" : {
"path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
},
"password" : "pqr",
"username" : "pqr"
}],
"timestamp" : 1425359738746,
"duration" : 15,
"organization" : "serv-d",
"applicationName" : "demo1",
"count" : 1
};
//logging username and password fields
console.log(data.entities[0].username, data.entities[0].password);
JSON behaves like key value pair kind of collection. Assign this Json to Javascript variable and then you can get the values of JSON with respective key like
var myJson =
{
"action" : "get", "application" : "2c5ca3b0-be74-11e4-8ff3-
f7af49a474ef", "params" : { "ql" : [ "select * where
username='pqr'" ] }, "path" : "/logs", "uri" :
"https://api.usergrid.com/serv-d/demo1/logs", "entities" : [
{ "uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e", "type" :
"log", "created" : 1425036869840, "modified" :
1425036869840, "metadata" :
{ "path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
}, "pas sword" : "pqr", "username" : "pqr"
}], "timestamp" : 1425359738746, "duration" : 15,
"organization" : "serv-d", "applicationName" : "demo1",
"count" : 1 };
var action = myJSON.action;
var uri = myJSON.uri; and so on..
Assuming you store it as "obj", then you can do obj.entities[0].username (and likewise for password.
Assuming you are referring it as data, You can use data.entities[0].username to access username.
var data = {
"action": "get",
"application": "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
"params": {
"ql": [
"select * where username='pqr'"
]
},
"path": "/logs",
"uri": "https://api.usergrid.com/serv-d/demo1/logs",
"entities": [
{
"uuid": "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
"type": "log",
"created": 1425036869840,
"modified": 1425036869840,
"metadata": {
"path": "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
},
"password": "pqr",
"username": "pqr"
}
],
"timestamp": 1425359738746,
"duration": 15,
"organization": "serv-d",
"applicationName": "demo1",
"count": 1
};
alert(data.entities[0].username)
alert(data.entities[0].password)
var yourData = {
"action" : "get",
"application" : "2c5ca3b0-be74-11e4-8ff3-f7af49a474ef",
"params" : {
"ql" : [ "select * where username='pqr'" ]
},
"path" : "/logs",
"uri" : "https://api.usergrid.com/serv-d/demo1/logs",
"entities" : [ {
"uuid" : "97b0fd0a-be74-11e4-9324-b3bd8af7859e",
"type" : "log",
"created" : 1425036869840,
"modified" : 1425036869840,
"metadata" : {
"path" : "/logs/97b0fd0a-be74-11e4-9324-b3bd8af7859e"
},
"password" : "pqr",
"username" : "pqr"
}],
"timestamp" : 1425359738746,
"duration" : 15,
"organization" : "serv-d",
"applicationName" : "demo1",
"count" : 1
}
Because username and password is data of entities- that is an array of one element member
you may access the username data
var username = yourData.entities[0].username
Similarly for password