V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
xcatliu
V2EX  ›  分享创造

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

  •  
  •   xcatliu ·
    xcatliu · 2018-04-09 19:17:42 +08:00 · 5394 次点击
    这是一个创建于 2180 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    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 }
    //     ]
    // }
    
    第 1 条附言  ·  2018-04-11 19:52:32 +08:00
    更新了一些功能:

    - `$disable` 用来选择性的禁用某个 field
    - `clearEmpty` 参数,用来清理空数据
    10 条回复    2018-08-10 08:05:20 +08:00
    natforum
        1
    natforum  
       2018-04-10 02:18:44 +08:00   ❤️ 1
    这个猫头是 mobi.css 的作者吧,印象比较深,mark
    xcatliu
        2
    xcatliu  
    OP
       2018-04-10 10:37:47 +08:00
    @natforum 是哒
    lcj2class
        3
    lcj2class  
       2018-04-10 21:06:43 +08:00
    好奇什么场景下会用到这种转化,之前没用过。
    sunwei0325
        4
    sunwei0325  
       2018-04-11 08:53:32 +08:00   ❤️ 1
    xcatliu
        5
    xcatliu  
    OP
       2018-04-11 19:49:02 +08:00
    @lcj2class 后端给的数据太乱了,前端整理一遍
    xcatliu
        6
    xcatliu  
    OP
       2018-04-11 19:49:19 +08:00
    @sunwei0325 看上去不错啊,先收藏了
    horizon
        7
    horizon  
       2018-04-12 13:47:32 +08:00
    @xcatliu 如果 foo.bar 为 undefin,foo.bar.baz 会 error 么
    xcatliu
        8
    xcatliu  
    OP
       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
        9
    xiaket  
       2018-08-09 08:22:02 +08:00   ❤️ 1
    这个需求的通用解决方案是 jq, 不过 jq 的那个 jmespath 实在太恶心人了.......
    ligyxy
        10
    ligyxy  
       2018-08-10 08:05:20 +08:00 via Android   ❤️ 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3001 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:43 · PVG 22:43 · LAX 07:43 · JFK 10:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.