首页 文章

JSON的XSLT等价物

提问于
浏览
334

JSON有XSLT等价物吗?允许我对XSON进行转换的事情,比如XSLT对XML做的转换 .

24 回答

  • 64

    JSONiq是一个开源C实现的标准和_125541 . 通过将JSON添加为本机数据类型,JSONiq也可以被视为XQuery .

  • 2

    使用XSLT转换JSON是非常可能的:您需要JSON2SAX反序列化器和SAX2JSON序列化器 .

    Java中的示例代码:http://www.gerixsoft.com/blog/json/xslt4json

  • 8

    jq - lightweight and flexible command-line JSON processor

    它不像XSLT那样基于模板,但更简洁 . 例如将 nameaddress 字段提取到数组中: [.name, .address]

    tutorial介绍了转换Twitter的JSON API的例子(manual有很多例子) .

  • 0

    JSON的XSLT等价物 - 候选列表(工具和规范)

    工具

    本节介绍允许使用XSLT处理JSON数据的工具 .

    • jq 上次提交2018年5月12日

    jq就像是用于JSON数据的sed - 您可以使用它来切片和过滤,映射和转换结构化数据,同样方便sed,awk,grep和朋友让你玩文本 . 有不同操作系统的安装包 .

    • jj 最后一次提交2018年3月2日

    JJ是一个命令行实用程序,它提供了一种快速简单的方法来从JSON文档中检索或更新值 . 它由GJSON和SJSON提供支持 .

    • fx 最后一次提交2018年3月20日

    命令行JSON处理工具不需要学习新语法普通JavaScript格式化和突出显示独立二进制文件

    • jsawk 上次提交2015年3月4日

    Jsawk就像awk,但对于JSON . 您使用从stdin读取的JSON对象数组,使用JavaScript过滤它们以生成打印到stdout的结果数组 .

    • json 最后提交2017年3月3日

    json是一个使用JSON的快速CLI工具 . 它是一个单文件node.js脚本,没有外部代码(除了node.js本身) .

    • jl 上次提交2018年4月10日

    jl(“JSON lambda”)是一种用于查询和操作JSON的小型函数语言 .

    • yate 最后提交2017年3月13日

    测试可以用作文档https://github.com/pasaran/yate/tree/master/tests

    • json-transforms 上次提交2017年12月1日

    提供递归的模式匹配方法来转换JSON数据 . 转换被定义为一组与JSON对象的结构匹配的规则 . 当匹配发生时,规则发出转换的数据,可选地递归以转换子对象 .

    • jsonpath-object-transform 最后提交2017年1月18日

    使用JSONPath从对象文字中提取数据,并基于模板生成新对象 .

    • JOLT 上次提交2018年5月1日

    用Java编写的JSON到JSON转换库,其中转换的“规范”本身就是一个JSON文档 .

    • gron 上次提交2018年4月5日

    使JSON可以抓取! gron将JSON转换为离散的赋值,以便更容易地为您想要的内容进行grep,并查看它的绝对“路径” . 它简化了对返回大量JSON但具有可怕文档的API的探索 .

    • Stapling 上次提交2013年9月16日

    Stapling是一个JavaScript库,可以为JSON对象启用XSLT格式 . Stapling不是使用JavaScript模板引擎和text / html模板,而是让您有机会使用XSLT模板 - 与Ajax异步加载,然后缓存客户端 - 来解析您的JSON数据源 .

    • json-e 上次提交2018年5月22日

    JSON-e是一个数据结构参数化系统,用于在JSON对象中嵌入上下文 . 中心思想是将数据结构视为“模板”,并使用另一种数据结构作为上下文对其进行转换,以生成输出数据结构 .

    规格:

    JSON指针定义了一种字符串语法,用于标识JavaScript Object Notation(JSON)文档中的特定值 .

    JSONPath表达式始终引用JSON结构,其方式与XPath表达式与XML文档结合使用的方式相同

    用于JSON的JSPath就像XPath for XML . “

    JSONiq背后的主要灵感来源是XQuery,它已被证明是迄今为止用于半结构化数据的成功且高效的查询语言

  • 0

    可以将XSLT与JSON一起使用 . XPath(3.1)的第3版XSLT(3.0)和XQuery(3.1)以某种方式支持JSON . 这似乎可以在Saxon的商业版本中获得,并且可能在某些时候包含在HE版本中 . https://www.saxonica.com/html/documentation/functions/fn/parse-json.html

    What I would expect from an alternative solution:

    我希望能够输入JSON来获取匹配的数据集,并输出JSON或TEXT .

    访问任意属性并评估值

    支持条件逻辑

    我希望转换脚本在工具,基于文本的情况下是外部的,最好是常用的语言 .

    Potential alternative?

    我想知道SQL是否是一个合适的选择 . https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server

    如果替代工具可以处理JSON和XML,那将会很好https://docs.microsoft.com/en-us/sql/relational-databases/xml/openxml-sql-server

    我还没有尝试将我使用的XSLT脚本转换为SQL,或者已经完全评估了这个选项,但我希望能够更快地研究它 . 到目前为止只是一些想法 .

  • 0

    有现在!我最近创建了一个库, json-transforms ,正是为了这个目的:

    https://github.com/ColinEberhardt/json-transforms

    它使用JSPath(一种基于XPath建模的DSL)和一种直接受XSLT启发的递归模式匹配方法的组合 .

    这是一个简单的例子 . 给定以下JSON对象:

    const json = {
      "automobiles": [
        { "maker": "Nissan", "model": "Teana", "year": 2011 },
        { "maker": "Honda", "model": "Jazz", "year": 2010 },
        { "maker": "Honda", "model": "Civic", "year": 2007 },
        { "maker": "Toyota", "model": "Yaris", "year": 2008 },
        { "maker": "Honda", "model": "Accord", "year": 2011 }
      ]
    };
    

    这是一个转变:

    const jsont = require('json-transforms');
    const rules = [
      jsont.pathRule(
        '.automobiles{.maker === "Honda"}', d => ({
          Honda: d.runner()
        })
      ),
      jsont.pathRule(
        '.{.maker}', d => ({
          model: d.match.model,
          year: d.match.year
        })
      ),
      jsont.identity
    ];
    
    const transformed  = jsont.transform(json, rules);
    

    哪个输出如下:

    {
      "Honda": [
        { "model": "Jazz", "year": 2010 },
        { "model": "Civic", "year": 2007 },
        { "model": "Accord", "year": 2011 }
      ]
    }
    

    该变换由三个规则组成 . 第一个匹配任何由本田制造的汽车,发射具有 Honda 属性的对象,然后递归匹配 . 第二个规则匹配具有 maker 属性的任何对象,输出 modelyear 属性 . 最后是递归匹配的身份转换 .

  • 21

    尚未给出的一种方法是使用解析器生成器在XSLT中创建解析器,该解析器解析JSON并生成XML输出 .

    在XML Session 上经常提到的一个选项是ReX解析器生成器(http://www.bottlecaps.de/rex/) - 虽然在网站上完全没有记录,但搜索时可以使用配方 .

  • 14

    很久以前,我为基于 Jackson 的json处理框架编写了一个dom适配器 . 它使用nu.xom库 . 生成的dom树与java xpath和xslt工具一起使用 . 我做了一些非常简单的实现选择 . 例如,根节点总是被称为“root”,数组进入具有li子元素的ol节点(如html中),而其他所有内容只是具有原始值的子节点或另一个对象节点 .

    JsonXmlConverter.java

    用法: JsonObject sampleJson = sampleJson(); org.w3c.dom.Document domNode = JsonXmlConverter.getW3cDocument(sampleJson, "root");

  • 14

    作为旧问题的另一个新答案,我建议看一下DefiantJS . 它不是JSON的XSLT等价物,它是JSL的 is XSLT . 文档的"Templating"部分包含以下示例:

    <!-- Defiant template -->
    <script type="defiant/xsl-template">
        <xsl:template name="books_template">
            <xsl:for-each select="//movie">
                <xsl:value-of select="title"/>
    </xsl:for-each> </xsl:template> </script> <script type="text/javascript"> var data = { "movie": [ {"title": "The Usual Suspects"}, {"title": "Pulp Fiction"}, {"title": "Independence Day"} ] }, htm = Defiant.render('books_template', data); console.log(htm); // The Usual Suspects<br> // Pulp Fiction<br> // Independence Day<br>
  • 0

    我最近找到了一个我喜欢的样式JSON工具:https://github.com/twigkit/tempo . 非常简单的工具 - 在我看来,它比XSLT更容易使用 - 不需要XPATH查询 .

  • 12

    Yate(https://github.com/pasaran/yate)是在XSLT之后专门设计的,具有JPath(JS的自然XPath等价物),编译为JavaScript并且具有相当的 生产环境 历史 . 它实际上没有文档,但阅读样本和测试应该足够了 .

  • 9

    不太确定是否需要这个,对我来说缺乏工具表明缺乏需求 . JSON最好作为对象处理(无论如何都是在JS中完成的),并且通常使用对象本身的语言来进行转换(Java用于从JSON创建的Java对象,对于Perl,Python,Perl,c#,PHP等都是如此)上) . 只需使用正常的赋值(或set,get),循环等等 .

    我的意思是,XSLT只是另一种语言,需要的一个原因是XML不是对象符号,因此编程语言的对象不是精确匹配(分层xml模型和对象/结构之间的阻抗) .

  • 58

    我正在使用Camel route umarshal(xmljson) - > to(xlst) - > marshal(xmljson) . 足够高效(虽然不是100%完美),但很简单,如果你已经在使用Camel .

  • 0

    有趣的想法 . 一些在Google上搜索产生了一些感兴趣的页面,包括:

    希望这可以帮助 .

  • 1

    有关使用纯JavaScript以及XSLT匹配表达式和递归模板背后的熟悉和声明模式的方法的工作涂鸦/概念证明,请参阅https://gist.github.com/brettz9/0e661b3093764f496e36

    (JSON可能采用类似的方法 . )

    请注意,该演示还依赖于JavaScript 1.8表达式闭包,以便在Firefox中表达模板(至少在实现方法的ES6简写形式之前) .

    免责声明:这是我自己的代码 .

  • 2

    为什么不使用Mr. Data Coverter将JSON转换为XML,使用XSLT转换它,然后使用相同的方法将其更改回JSON .

  • 1

    试试JOLT . 它是用Java编写的JSON to JSON转换库 .

    它是专门创建的,因为我们不想播放“JSON - > XML - > XSLT - > XML - > JSON”游戏,并且使用模板进行任何足够复杂的转换都是不可维护的 .

  • 10

    说缺乏工具表明缺乏需求只是乞求这个问题 . 同样可以用于支持Linux中的X或Y(为什么要为这样的少数操作系统开发高质量的驱动程序和/或游戏?为什么要关注大型游戏和硬件公司不开发的操作系统?) . 可能需要使用XSLT和JSON的人最终会使用一些简单的解决方法:将JSON转换为XML . 但这不是最佳解决方案,是吗?

    当你有一个土生土长的人JSON格式,你想在浏览器中编辑它“wysywyg”,XSLT将是一个绰绰有余的解决方案 . 使用传统的javascript编程实现这一点可能会成为一种痛苦 .

    事实上,我已经实现了对XSLT的“石器时代”方法,使用子字符串解析来解释javascript的一些基本命令,比如调用模板,处理子进程等 . 当然,使用JSON对象实现转换引擎要比实现一个完整的XML解析器来解析XSLT . 问题是,要使用XML模板转换JSON对象,需要解析模板的XML .

    要使用XML(或HTML,或文本或其他)转换JSON对象,您需要仔细考虑语法以及需要使用哪些特殊字符来标识转换命令 . 否则,您最终将不得不为自己的自定义模板语言设计解析器 . 走过这条路,我可以告诉你,它并不漂亮 .

    更新(2010年11月12日):经过几周的解析器工作,我已经能够优化它 . 事先解析模板,并将命令存储为JSON对象 . 转换规则也是JSON对象,而模板代码是HTML和类似于shell代码的自制语法的混合 . 我已经能够将复杂的JSON文档转换为HTML来制作文档编辑器 . 代码大约是1K行的编辑器(它用于私有项目,所以我不能共享它)和大约990行用于JSON转换代码(包括迭代命令,简单比较,模板调用,变量保存和评估) . 我计划在MIT许可下发布它 . 如果你想参与,请给我发邮件 .

  • 13

    最近我在这周围写了我自己的小图书馆,试图保持尽可能接近

    5.1处理模型(XSLT REC)https://www.w3.org/TR/xslt#section-Processing-Model

    尽可能(尽管我可以),在几行JavaScript代码中 .

    以下是一些使用不完全琐碎的例子......

    1. JSON-to-some-markup:

    小提琴:https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10

    (受D.1文档示例(XSLT REC)启发https://www.w3.org/TR/xslt#section-Document-Example

    这个:

    var D1document = {
        type: "document", title: [ "Document Title" ],
        "": [
          { type: "chapter", title: [ "Chapter Title" ],
            "": [
            { type: "section", title: [ "Section Title" ],
              "": [
                { type: "para", "": [ "This is a test." ] },
                { type: "note", "": [ "This is a note." ] }
            ] },
            { type: "section", title: [ "Another Section Title" ],
              "": [
                { type: "para", "": [ "This is ", { emph: "another" }, " test." ] },
                { type: "note", "": [ "This is another note." ] }
            ] }
          ] }
        ] };
    
    var D1toHTML = { $: [
      [ [ function(node) { return node.type === "document"; } ],
        function(root) {
          return "<html>\r\n\
      <head>\r\n\
        <title>\r\n\
          {title}\r\n".of(root) + "\
        </title>\r\n\
      </head>\r\n\
      <body>\r\n\
    {*}".of(root[""].through(this)) + "\
      </body>\r\n\
    </html>";
        }
      ],
      [ [ function(node) { return node.type === "chapter"; } ],
        function(chapter) {
          return "    <h2>{title}</h2>\r\n".of(chapter) + "{*}".of(chapter[""].through(this));
        }
      ],
      [ [ function(node) { return node.type === "section"; } ],
        function(section) {
          return "    <h3>{title}</h3>\r\n".of(section) + "{*}".of(section[""].through(this));
        }
      ],
      [ [ function(node) { return node.type === "para"; } ],
        function(para) {
          return "    <p>{*}</p>\r\n".of(para[""].through(this));
        }
      ],
      [ [ function(node) { return node.type === "note"; } ],
        function(note) {
          return '    <p class="note"><b>NOTE: </b>{*}</p>\r\n'.of(note[""].through(this));
        }
      ],
      [ [ function(node) { return node.emph; } ],
        function(emph) {
          return "<em>{emph}</em>".of(emph);
        }
      ]
    ] };
    
    console.log(D1document.through(D1toHTML));
    

    ......给出:

    <html>
      <head>
        <title>
          Document Title
        </title>
      </head>
      <body>
        <h2>Chapter Title</h2>
        <h3>Section Title</h3>
        <p>This is a test.</p>
        <p class="note"><b>NOTE: </b>This is a note.</p>
        <h3>Another Section Title</h3>
        <p>This is <em>another</em> test.</p>
        <p class="note"><b>NOTE: </b>This is another note.</p>
      </body>
    </html>
    

    2. JSON-to-JSON:

    小提琴:https://jsfiddle.net/YSharpLanguage/ppfmmu15/10

    这个:

    // (A "Company" is just an object with a "Team")
    function Company(obj) {
      return obj.team && Team(obj.team);
    }
    
    // (A "Team" is just a non-empty array that contains at least one "Member")
    function Team(obj) {
      return ({ }.toString.call(obj) === "[object Array]") &&
             obj.length &&
             obj.find(function(item) { return Member(item); });
    }
    
    // (A "Member" must have first and last names, and a gender)
    function Member(obj) {
      return obj.first && obj.last && obj.sex;
    }
    
    function Dude(obj) {
      return Member(obj) && (obj.sex === "Male");
    }
    
    function Girl(obj) {
      return Member(obj) && (obj.sex === "Female");
    }
    
    var data = { team: [
      { first: "John", last: "Smith", sex: "Male" },
      { first: "Vaio", last: "Sony" },
      { first: "Anna", last: "Smith", sex: "Female" },
      { first: "Peter", last: "Olsen", sex: "Male" }
    ] };
    
    var TO_SOMETHING_ELSE = { $: [
    
      [ [ Company ],
        function(company) {
          return { some_virtual_dom: {
            the_dudes: { ul: company.team.select(Dude).through(this) },
            the_grrls: { ul: company.team.select(Girl).through(this) }
          } }
        } ],
    
      [ [ Member ],
        function(member) {
          return { li: "{first} {last} ({sex})".of(member) };
        } ]
    
    ] };
    
    console.log(JSON.stringify(data.through(TO_SOMETHING_ELSE), null, 4));
    

    ......给出:

    {
        "some_virtual_dom": {
            "the_dudes": {
                "ul": [
                    {
                        "li": "John Smith (Male)"
                    },
                    {
                        "li": "Peter Olsen (Male)"
                    }
                ]
            },
            "the_grrls": {
                "ul": [
                    {
                        "li": "Anna Smith (Female)"
                    }
                ]
            }
        }
    }
    

    3. XSLT与JavaScript:

    相当于......的JavaScript

    XSLT 3.0 REC Section 14.4 Example: Grouping Nodes based on Common Values

    (at:http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1

    参看https://www.w3.org/TR/xslt-30/#grouping-examples

    哪里...

    var cities = [
      { name: "Milano",  country: "Italia",      pop: 5 },
      { name: "Paris",   country: "France",      pop: 7 },
      { name: "München", country: "Deutschland", pop: 4 },
      { name: "Lyon",    country: "France",      pop: 2 },
      { name: "Venezia", country: "Italia",      pop: 1 }
    ];
    
    /*
      Cf.
      XSLT 3.0 REC Section 14.4
      Example: Grouping Nodes based on Common Values
    
      https://www.w3.org/TR/xslt-30/#grouping-examples
    */
    var output = "<table>\r\n\
      <tr>\r\n\
        <th>Position</th>\r\n\
        <th>Country</th>\r\n\
        <th>City List</th>\r\n\
        <th>Population</th>\r\n\
      </tr>{*}\r\n\
    </table>".of
      (
        cities.select().groupBy("country")(function(byCountry, index) {
          var country = byCountry[0],
              cities = byCountry[1].select().orderBy("name");
          return "\r\n\
      <tr>\r\n\
        <td>{position}</td>\r\n\
        <td>{country}</td>\r\n\
        <td>{cities}</td>\r\n\
        <td>{population}</td>\r\n\
      </tr>".
            of({ position: index + 1, country: country,
                 cities: cities.map(function(city) { return city.name; }).join(", "),
                 population: cities.reduce(function(sum, city) { return sum += city.pop; }, 0)
               });
        })
      );
    

    ......给出:

    <table>
      <tr>
        <th>Position</th>
        <th>Country</th>
        <th>City List</th>
        <th>Population</th>
      </tr>
      <tr>
        <td>1</td>
        <td>Italia</td>
        <td>Milano, Venezia</td>
        <td>6</td>
      </tr>
      <tr>
        <td>2</td>
        <td>France</td>
        <td>Lyon, Paris</td>
        <td>9</td>
      </tr>
      <tr>
        <td>3</td>
        <td>Deutschland</td>
        <td>München</td>
        <td>4</td>
      </tr>
    </table>
    

    4. JSONiq与JavaScript:

    相当于......的JavaScript

    JSONiq Use Cases Section 1.1.2. Grouping Queries for JSON

    (at:https://jsfiddle.net/YSharpLanguage/hvo24hmk/3

    参看http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping

    哪里...

    /*
      1.1.2. Grouping Queries for JSON
      http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
    */
    var sales = [
      { "product" : "broiler", "store number" : 1, "quantity" : 20  },
      { "product" : "toaster", "store number" : 2, "quantity" : 100 },
      { "product" : "toaster", "store number" : 2, "quantity" : 50 },
      { "product" : "toaster", "store number" : 3, "quantity" : 50 },
      { "product" : "blender", "store number" : 3, "quantity" : 100 },
      { "product" : "blender", "store number" : 3, "quantity" : 150 },
      { "product" : "socks", "store number" : 1, "quantity" : 500 },
      { "product" : "socks", "store number" : 2, "quantity" : 10 },
      { "product" : "shirt", "store number" : 3, "quantity" : 10 }
    ];
    
    var products = [
      { "name" : "broiler", "category" : "kitchen", "price" : 100, "cost" : 70 },
      { "name" : "toaster", "category" : "kitchen", "price" : 30, "cost" : 10 },
      { "name" : "blender", "category" : "kitchen", "price" : 50, "cost" : 25 },
      {  "name" : "socks", "category" : "clothes", "price" : 5, "cost" : 2 },
      { "name" : "shirt", "category" : "clothes", "price" : 10, "cost" : 3 }
    ];
    
    var stores = [
      { "store number" : 1, "state" : "CA" },
      { "store number" : 2, "state" : "CA" },
      { "store number" : 3, "state" : "MA" },
      { "store number" : 4, "state" : "MA" }
    ];
    
    var nestedGroupingAndAggregate = stores.select().orderBy("state").groupBy("state")
    ( function(byState) {
        var state = byState[0],
            stateStores = byState[1];
        byState = { };
        return (
          (
            byState[state] =
            products.select().orderBy("category").groupBy("category")
            ( function(byCategory) {
                var category = byCategory[0],
                    categoryProducts = byCategory[1],
                    categorySales = sales.filter(function(sale) {
                      return stateStores.find(function(store) { return sale["store number"] === store["store number"]; }) &&
                             categoryProducts.find(function(product) { return sale.product === product.name; });
                    });
                byCategory = { };
                return (
                  (
                    byCategory[category] =
                    categorySales.select().orderBy("product").groupBy("product")
                    ( function(byProduct) {
                        var soldProduct = byProduct[0],
                            soldQuantities = byProduct[1];
                        byProduct = { };
                        return (
                          (
                            byProduct[soldProduct] =
                            soldQuantities.reduce(function(sum, sale) { return sum += sale.quantity; }, 0)
                          ),
                          byProduct
                        );
                    } ) // byProduct()
                  ),
                  byCategory
                );
            } ) // byCategory()
          ),
          byState
        );
    } ); // byState()
    

    ......给出:

    [
      {
        "CA": [
          {
            "clothes": [
              {
                "socks": 510
              }
            ]
          },
          {
            "kitchen": [
              {
                "broiler": 20
              },
              {
                "toaster": 150
              }
            ]
          }
        ]
      },
      {
        "MA": [
          {
            "clothes": [
              {
                "shirt": 10
              }
            ]
          },
          {
            "kitchen": [
              {
                "blender": 250
              },
              {
                "toaster": 50
              }
            ]
          }
        ]
      }
    ]
    

    克服JSONPath wrt的局限性也很有用 . 查询祖先轴,由this SO question(当然还有其他人)提出 .

    例如,如何获得知道其品牌ID的杂货商品的折扣

    {
     "prods": [
        {
            "info": {
                  "rate": 85
                    },
            "grocery": [
                     {
                      "brand": "C",
                      "brand_id": "984"
                     },
                     {
                      "brand": "D",
                      "brand_id": "254"
                     }
                     ],
             "discount": "15"
        },
        {
            "info": {
                  "rate": 100
                    },
            "grocery": [
                     {
                      "brand": "A",
                      "brand_id": "983"
                     },
                     {
                      "brand": "B",
                      "brand_id": "253"
                     }
                     ],
             "discount": "20"
         }
     ]
    }
    

    可能的解决方案是:

    var products = {
         "prods": [
            {
                "info": {
                      "rate": 85
                        },
                "grocery": [
                         {
                          "brand": "C",
                          "brand_id": "984"
                         },
                         {
                          "brand": "D",
                          "brand_id": "254"
                         }
                         ],
                 "discount": "15"
            },
            {
                "info": {
                      "rate": 100
                        },
                "grocery": [
                         {
                          "brand": "A",
                          "brand_id": "983"
                         },
                         {
                          "brand": "B",
                          "brand_id": "253"
                         }
                         ],
                 "discount": "20"
             }
         ]
    };
    
    function GroceryItem(obj) {
      return (typeof obj.brand === "string") && (typeof obj.brand_id === "string");
    }
    
        // last parameter set to "true", to grab all the "GroceryItem" instances
        // at any depth:
    var itemsAndDiscounts = [ products ].nodeset(GroceryItem, true).
        map(
          function(node) {
            var item = node.value, // node.value: the current "GroceryItem" (aka "$.prods[*].grocery[*]")
    
                discount = node.parent. // node.parent: the array of "GroceryItem" (aka "$.prods[*].grocery")
                           parent. // node.parent.parent: the product (aka "$.prods[*]")
                           discount; // node.parent.parent.discount: the product discount
    
            // finally, project into an easy-to-filter form:
            return { id: item.brand_id, discount: discount };
          }
        ),
        discountOfItem983;
    
    discountOfItem983 = itemsAndDiscounts.
      filter
      (
        function(mapped) {
          return mapped.id === "983";
        }
      )
      [0].discount;
    
    console.log("Discount of #983: " + discountOfItem983);
    

    ... 这使:

    Discount of #983: 20
    

    “HTH,

  • 8

    我已经厌倦了大量的JavaScript模板引擎,以及所有内联HTML模板,不同的标记样式等,并决定build a small library为JSON数据结构启用XSLT格式化 . 它不是火箭科学 - 它也快,不像Chrome中的JavaScript模板引擎那么快,但在大多数其他浏览器中,它至少与大型数据结构的JS引擎替代一样快 .

  • 1

    XSLT支持JSON,如http://www.w3.org/TR/xslt-30/#json所示

    XML使用尖括号作为分隔符令牌,JSON使用大括号,方括号,...... I. e . XML更少的令牌识别比较意味着它针对声明性转换进行了优化,而更多的比较,如switch语句,出于速度原因,假设推测分支预测脚本语言中的命令性代码是有用的 . 直接结果是,对于半结构化数据的不同组合,您可能希望将XSLT和javascript引擎的性能作为响应式页面的一部分进行基准测试 . 对于可忽略的数据有效负载,转换可能与没有XML序列化的JSON一样有效 . W3的决定应该基于更好的分析 .

  • 0
  • 4

    JSON-e在Node.js,Python和Go中有实现 .

  • 0

    JSLT非常接近JSON等效的XSLT . 它是一种转换语言,您可以使用JSON语法编写输出的固定部分,然后插入表达式以计算要在模板中插入的值 .

    一个例子:

    {
      "time": round(parse-time(.published, "yyyy-MM-dd'T'HH:mm:ssX") * 1000),
      "device_manufacturer": .device.manufacturer,
      "device_model": .device.model,
      "language": .device.acceptLanguage
    }
    

    它是在Jackson上用Java实现的 .

相关问题