dregenor / jsonmapper Goto Github PK
View Code? Open in Web Editor NEWsimple json mapper
License: MIT License
simple json mapper
License: MIT License
Hi, thanks for your working, given codes:
const _jmap = require('json-mapper');
const _h = _jmap.helpers;
const json = { a: 1, b: 2, c: 3 };
const mapping = {
x: "c", y: "b", z: "a",
arr: [
{ k: _h.def('m'), v: _h.def(2) },
{ k: _h.def('n'), v: _h.def(4) }
]
};
const conv = _jmap.makeConverter(mapping);
const ret = conv(json);
console.log(ret);
{x:3,y:2,z:1,arr:{k:'n',v:4}}
I hope the arr
in the mapping would keep as it was, but it would return just the last element, how to correct the codes?
Is it possible to call JM.ch
into dict
prcedure?
Is there a way to use json-mapper to determine the unmapped residue? That is, if I create a converter and I map a bunch of fields from one JSON format into a target format, is there a way to get a list of fields that weren't converted (i.e. dropped) from the input?
Is there any way to run a converter on an array of objects and return an array of converted objects? For example:
var input = [
{
a: '1',
b: '2'
},
{
a: '3',
b: '4'
}
];
var objConverter = jm.makeConverter({
a: 'a'
});
var arrayConverter = jm.makeConverter([
// I want to map the objConverter to each object in input
]);
arrayConverter(input);
/*
Expected result:
[
{
a: '1'
},
{
a: '3'
}
]
*/
When you call makeConverter
on an object, the implementation of that function actually destructively modifies the source object.
Sample test case to show the issue:
> var jm = require('json-mapper');
undefined
> var myConverter = { 'foo': 'bar', subDocument: { hello: 'world' } };
undefined
> var converter = jm.makeConverter(myConverter);
undefined
> myConverter.foo
[Function]
> myConverter.subDocument
[Function]
>
The source code that modifies the original object is here
This was the cause of a quite difficult bug; in my application we create mapping classes and we have other uses for those mapping classes besides feeding them into JSON-mapper to create converters. The bug we were seeing was because json-mapper had replaced an object definition (subDocument
above) with a function, it's own callback.
Principle of least surprise is that the module should not modify its arguments, instead returning a completely new object leaving the original mapping specification untouched. This makes it possible for us to keep using our data structures as-is.
Our present work-around is to deep clone the objects we have before we give them to json-mapper, so that we can retain a copy without modification.
Hi,
Is there a way to map out a nested json array, so i can keep references of the child array to its parent while cycling through both?
I've tried using the map feature but within the second map the value passed doesn't seem to quite work.
given an example json:
{
"schools" : [
{
"name" : "school a",
"abbvr" : "AED",
"students" : [
{"name" : "a"},
{"name" : "b"}
]
},
{
"name" : "school b",
"abbvr" : "EG",
"students" : [
{"name" : "c"},
{"name" : "d"}
]
},
{
"name" : "school c",
"abbvr" : "ESA",
"students" : [
{"name" : "e"},
{"name" : "f"}
]
}
]
}
The end goal being to cycle through each school and then cycle through each student as well.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.