分享将一个 json 转为另一个 json 的方法

2018-04-09 19:17:42 +08:00
 xcatliu

尝试了一些现有的模块,都不太好用,干脆自己又造了个轮子。欢迎试用

GitHub 地址: https://github.com/xcatliu/awesome-json2json

An awesome json to json mapper

Usage

import json2json from 'awesome-json2json';
// const json2json = require('awesome-json2json').default;

json2json({ foo: { bar: { baz: 1 }}}, {
    new_foo: 'foo.bar.baz'
});
// { new_foo: 1 }

Features

Optional chaining

json2json({ foo: { bar: { baz: 1 }}}, {
    new_foo: 'foo.not_exist_key?.bar.baz'
});
// { new_foo: undefined }

Function template

json2json({ foo: { bar: { baz: 1 }}}, {
    new_foo: (root) => {
        return root.foo.bar.baz + '_formatted';
    }
});
// { new_foo: '1_formatted' }

Template with $path and $formatting

json2json({ foo: { bar: { baz: 1 }}}, {
    new_foo: {
        $path: 'foo.bar',
        $formatting: (bar) => {
            return bar.baz + '_formatted';
        }
    }
});
// { new_foo: '1_formatted' }

Template with nested template

json2json({ foo: { bar: { baz: 1 }}}, {
    new_foo: {
        $path: 'foo',
        new_bar: 'bar.baz'
    }
});
// { new_foo: { new_bar: 1 }}

Template with nested template with $path and $formatting

json2json({ foo: { bar: { baz: 1 }}}, {
    new_foo: {
        $path: 'foo',
        $formatting: (foo) => {
            return {
                baz2: foo.bar.baz + '_formatted'
            }
        },
        new_bar: 'baz2'
    }
});
// { new_foo: { new_bar: '1_formatted' }}

Template with $root

json2json({ foo: { bar: { baz: 1 }}}, {
    new_foo: {
        $path: 'foo',
        new_bar: {
            $path: 'bar',
            new_baz1: 'baz',
            new_baz2: '$root.foo'
        }
    }
});
// new_foo: {
//     new_bar: {
//         new_baz1: 1,
//         new_baz2: {
//             bar: {
//                 baz: 1
//             }
//         }
//     }
// }

Array template

json2json({
    foo: [
        { bar: 1 },
        { bar: 2 },
        { bar: 3 }
    ]
}, {
    new_foo: 'foo[].bar'
});
// { new_foo: [1, 2, 3] }

Array template with formatting

json2json({
    foo: [
        { bar: 1 },
        { bar: 2 },
        { bar: 3 }
    ]
}, {
    new_foo: {
        $path: 'foo[].bar',
        $formatting: (barValue) => barValue + '_formatted'
    }
});
// {
//     new_foo: [
//         '1_formatted',
//         '2_formatted',
//         '3_formatted'
//     ]
// }

Array template with nested template

json2json({
    foo: [
        { bar: 1 },
        { bar: 2 },
        { bar: 3 }
    ]
}, {
    new_foo: {
        $path: 'foo[]',
        new_bar: {
            $formatting: (fooItem) => {
                return fooItem.bar;
            }
        }
    }
});
// {
//     new_foo: [
//         { new_bar: 1 },
//         { new_bar: 2 },
//         { new_bar: 3 }
//     ]
// }
5415 次点击
所在节点    分享创造
10 条回复
natforum
2018-04-10 02:18:44 +08:00
这个猫头是 mobi.css 的作者吧,印象比较深,mark
xcatliu
2018-04-10 10:37:47 +08:00
@natforum 是哒
lcj2class
2018-04-10 21:06:43 +08:00
好奇什么场景下会用到这种转化,之前没用过。
sunwei0325
2018-04-11 08:53:32 +08:00
xcatliu
2018-04-11 19:49:02 +08:00
@lcj2class 后端给的数据太乱了,前端整理一遍
xcatliu
2018-04-11 19:49:19 +08:00
@sunwei0325 看上去不错啊,先收藏了
horizon
2018-04-12 13:47:32 +08:00
@xcatliu 如果 foo.bar 为 undefin,foo.bar.baz 会 error 么
xcatliu
2018-04-12 14:01:59 +08:00
@horizon 会 throw error
如果不希望 throw error,可以使用 Optional chaining
foo.bar?.baz

https://github.com/xcatliu/awesome-json2json#optional-chaining
xiaket
2018-08-09 08:22:02 +08:00
这个需求的通用解决方案是 jq, 不过 jq 的那个 jmespath 实在太恶心人了.......
ligyxy
2018-08-10 08:05:20 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/445443

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX