
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
json-transpose
Advanced tools
Transposes an object to another object using templated fields. Could not find a library that could safely reference fields from the source object that may not exist and compile the mapping functions for later use.
The library is written using typescript, make sure to have to built the repository when testing as tests run off the final .js files
const { compile } = require('json-transpose');
const transform = compile({
someValue: '${it.some.nested.value}'
});
const result = transform({
some: {
nested: {
value: 'test'
}
}
});
console.log(result);
Output:
{
someValue: 'test'
}
{
...
[target field]: '${it.[path to source field]}',
...
}
Effectively you can do whatever JavaScript string templates can do.
{
...
[target field 1]: 'This is a template ${it.[path to source field]}',
[target field 2]: 'Some arithmatic ${it.[path to source field] + it.[path to source field]}',
...
}
If the source will be an array and you don't want to transpose any of the fields just reference the array:
{
...
target array: '${it.[path to source array]}'
...
}
To transpose the items within the array use an array for the definition which will take two items:
{
...,
[target array]: {
$array: {
source: 'it.[path to source array]',
item: {
someValue: '${it.some.nested.value}'
}
}
}
...
}
If the expression is the only thing in the template, the type it evaluates to will be the value assigned to the field.
Input
{
...,
string: 'Value: ${it.value}',
integer: '${it.value + 10}',
boolean: '${it.value > 10 && it.value < 20}'
...
}
Output:
{
...,
string: 'Value: 5',
integer: 15,
boolean: false
...
}
Certain objects have been supplied as helpers for accessing data that would otherwise be inaccessible.
The root object that was passed in. This is useful when needing to reference the root document within arrays.
{
...,
[target array]: {
$array: {
source: 'it.[path to source array]',
item: {
rootValue: '${_root.value}',
someValue: '${it.some.nested.value}'
}
}
}
...
}
Custom objects can be projected into expressions. json-transpose will attempt to filter out any non-field objects / operations. However, specific objects can be allowed for use.
const transform = compile({
trucatedNumber: '${Math.trunc(it.number)}',
formattedNumber: '${formatNumber(it.number)}'
}, {
customObjects: {
Math,
formatNumber(number) {
return `$${number.toFixed(2)}`;
}
}
});
transform({
number: 15.2345
});
Output:
{
truncatedNumber: 15,
formattedNumber: '$15.23'
}
It is okay to just have a string value for a template rather than a whole object.
const transform = compile('${it.some.nested.value}');
const result = transform({
some: {
nested: {
value: 'test'
}
}
});
Output:
'test'
If the value does not exist given the path for the field. The value will be set to null
const transform = compile({
someValue: '${it.some.nested.value}'
});
const result = transform({
some: { }
});
Output:
{
someValue: null
}
npm run test
FAQs
JSON Transpose ====================================
The npm package json-transpose receives a total of 861 weekly downloads. As such, json-transpose popularity was classified as not popular.
We found that json-transpose demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.