V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
爱意满满的作品展示区。
xcatliu

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

  •  
  •   xcatliu ·
    xcatliu · Apr 9, 2018 · 5948 views
    This topic created in 2951 days ago, the information mentioned may be changed or developed.

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

    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 }
    //     ]
    // }
    
    Supplement 1  ·  Apr 11, 2018
    更新了一些功能:

    - `$disable` 用来选择性的禁用某个 field
    - `clearEmpty` 参数,用来清理空数据
    10 replies    2018-08-10 08:05:20 +08:00
    natforum
        1
    natforum  
       Apr 10, 2018   ❤️ 1
    这个猫头是 mobi.css 的作者吧,印象比较深,mark
    xcatliu
        2
    xcatliu  
    OP
       Apr 10, 2018
    @natforum 是哒
    lcj2class
        3
    lcj2class  
       Apr 10, 2018
    好奇什么场景下会用到这种转化,之前没用过。
    sunwei0325
        4
    sunwei0325  
       Apr 11, 2018   ❤️ 1
    xcatliu
        5
    xcatliu  
    OP
       Apr 11, 2018
    @lcj2class 后端给的数据太乱了,前端整理一遍
    xcatliu
        6
    xcatliu  
    OP
       Apr 11, 2018
    @sunwei0325 看上去不错啊,先收藏了
    horizon
        7
    horizon  
       Apr 12, 2018
    @xcatliu 如果 foo.bar 为 undefin,foo.bar.baz 会 error 么
    xcatliu
        8
    xcatliu  
    OP
       Apr 12, 2018
    @horizon 会 throw error
    如果不希望 throw error,可以使用 Optional chaining
    foo.bar?.baz

    https://github.com/xcatliu/awesome-json2json#optional-chaining
    xiaket
        9
    xiaket  
       Aug 9, 2018   ❤️ 1
    这个需求的通用解决方案是 jq, 不过 jq 的那个 jmespath 实在太恶心人了.......
    ligyxy
        10
    ligyxy  
       Aug 10, 2018 via Android   ❤️ 1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2693 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 16:05 · PVG 00:05 · LAX 09:05 · JFK 12:05
    ♥ Do have faith in what you're doing.