首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以使用jq/yq/xq从JSON或YAML转换为XML

是否可以使用jq/yq/xq从JSON或YAML转换为XML
EN

Stack Overflow用户
提问于 2022-07-19 13:56:21
回答 2查看 785关注 0票数 0

我已经成功地使用XMLYAML文件转换为xq文件。

是否可以使用以下工具jqyqxqYAMLJSON转换回XML格式?

下面是我的示例JSON文件的示例:

代码语言:javascript
运行
复制
{
  "security-settings": {
    "@xmlns": "urn:activemq:core",
    "security-setting": {
      "@match": "#",
      "permission": [
        {
          "@type": "createNonDurableQueue",
          "@roles": "admins"
        },
        {
          "@type": "deleteNonDurableQueue",
          "@roles": "admins"
        },
        {
          "@type": "manage",
          "@roles": "admins"
        }
      ]
    }
  }
}

感谢您的任何帮助或建议。

附加信息:

我最初使用的源XML如下:

代码语言:javascript
运行
复制
<?xml version="1.0"?>
<security-settings xmlns="urn:activemq:core">
  <security-setting match="#">
    <permission type="createNonDurableQueue" roles="admins"/>
    <permission type="deleteNonDurableQueue" roles="admins"/>
    <permission type="createDurableQueue" roles="admins"/>
    <permission type="deleteDurableQueue" roles="admins"/>
    <permission type="createAddress" roles="admins"/>
    <permission type="deleteAddress" roles="admins"/>
    <permission type="consume" roles="admins"/>
    <permission type="browse" roles="admins"/>
    <permission type="send" roles="admins"/>
    <permission type="manage" roles="admins"/>
  </security-setting>
</security-settings>

XMLJSON的前向转换使用xq -yY < security-settings.xml命令生成JSON输出:

代码语言:javascript
运行
复制
{
  "security-settings": {
    "@xmlns": "urn:activemq:core",
    "security-setting": {
      "@match": "#",
      "permission": [
        {
          "@type": "createNonDurableQueue",
          "@roles": "admins"
        },
        {
          "@type": "deleteNonDurableQueue",
          "@roles": "admins"
        },
        {
          "@type": "createDurableQueue",
          "@roles": "admins"
        },
        {
          "@type": "deleteDurableQueue",
          "@roles": "admins"
        },
        {
          "@type": "createAddress",
          "@roles": "admins"
        },
        {
          "@type": "deleteAddress",
          "@roles": "admins"
        },
        {
          "@type": "consume",
          "@roles": "admins"
        },
        {
          "@type": "browse",
          "@roles": "admins"
        },
        {
          "@type": "send",
          "@roles": "admins"
        },
        {
          "@type": "manage",
          "@roles": "admins"
        }
      ]
    }
  }
}

运行yq -o=xml -P json_file为从JSONXML的后向转换所建议的本机转换不会产生与前面所示的源XML相同的结果。

代码语言:javascript
运行
复制
<security-settings>
  <@xmlns>urn:activemq:core</@xmlns>
  <security-setting>
    <@match>#</@match>
    <permission>
      <@type>createNonDurableQueue</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>deleteNonDurableQueue</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>createDurableQueue</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>deleteDurableQueue</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>createAddress</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>deleteAddress</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>consume</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>browse</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>send</@type>
      <@roles>admins</@roles>
    </permission>
    <permission>
      <@type>manage</@type>
      <@roles>admins</@roles>
    </permission>
  </security-setting>
</security-settings>

我正在一台Fedora 36虚拟机上运行,这是我在这个机器上安装的yq。

代码语言:javascript
运行
复制
yq --version
yq 3.0.2

yq --help
usage: yq [options] <jq filter> [input file...]

yq: Command-line YAML processor - jq wrapper for YAML documents

yq transcodes YAML documents to JSON and passes them to jq.
See https://github.com/kislyuk/yq for more information.

positional arguments:
  jq_filter
  files

options:
  -h, --help            show this help message and exit
  --yaml-output, --yml-output, -y
                        Transcode jq JSON output back into YAML and emit it
  --yaml-roundtrip, --yml-roundtrip, -Y
                        Transcode jq JSON output back into YAML and emit it. Preserve YAML tags and styles by representing them as extra items in their enclosing mappings and sequences while in JSON. This option is incompatible with jq filters that do not expect these extra items.
  --width WIDTH, -w WIDTH
                        When using --yaml-output, specify string wrap width
  --indentless-lists, --indentless
                        When using --yaml-output, indent block style lists (sequences) with 0 spaces instead of 2
  --in-place, -i        Edit files in place (no backup - use caution)
  --version             show program's version number and exit

jq - commandline JSON processor [version 1.6]

Usage:  jq [options] <jq filter> [file...]
    jq [options] --args <jq filter> [strings...]
    jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Example:

    $ echo '{"foo": 0}' | jq .
    {
        "foo": 0
    }

Some of the options include:
  -c               compact instead of pretty-printed output;
  -n               use `null` as the single input value;
  -e               set the exit status code based on the output;
  -s               read (slurp) all inputs into an array; apply filter to it;
  -r               output raw strings, not JSON texts;
  -R               read raw strings, not JSON texts;
  -C               colorize JSON;
  -M               monochrome (don't colorize JSON);
  -S               sort keys of objects on output;
  --tab            use tabs for indentation;
  --arg a v        set variable $a to value <v>;
  --argjson a v    set variable $a to JSON value <v>;
  --slurpfile a f  set variable $a to an array of JSON texts read from <f>;
  --rawfile a f    set variable $a to a string consisting of the contents of <f>;
  --args           remaining arguments are string arguments, not files;
  --jsonargs       remaining arguments are JSON arguments, not files;
  --               terminates argument processing;

Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].

See the manpage for more options.

@ikegami

这是输出:

echo ele_value xq

代码语言:javascript
运行
复制
{
  "ele": {
    "@attr_name": "attr_value",
    "#text": "ele_value"
  }
}

echo ele_value _ xq /yq_linux_ele 64 -o=xml -P

代码语言:javascript
运行
复制
<ele>
  <@attr_name>attr_value</@attr_name>
  <#text>ele_value</#text>
</ele>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-19 17:29:20

首先,从这个github存储库获得yq 的版本:amd64

它提供了其他人可能不支持的标志/特性。

然后,可以使用以下命令重新生成:

代码语言:javascript
运行
复制
./yq_linux_amd64               \
    --xml-attribute-prefix @   \
    --xml-content-name '#text' \
    --input-format yaml        \
    --output-format xml        \
    security-settings.yaml

同样的命令也适用于JSON输入,因为JSON是YAML的一个子集。

票数 3
EN

Stack Overflow用户

发布于 2022-07-19 16:54:22

jq:

代码语言:javascript
运行
复制
"@"     as $attr_prefix |
"#text" as $content_key |

# ">" only needs to be escaped if preceded by "]]".
# Some whitespace also need escaping, at least in attribute.
{ "&": "&amp;", "<": "&lt;", ">": "&gt;"    } as $escapes      |
{ "&": "&amp;", "<": "&lt;", "\"": "&quot;" } as $attr_escapes |

def text_to_xml:          split( "" ) | map( $escapes[.]       // . ) | join( "" );
def text_to_xml_attr_val: split( "" ) | map( $attr_escapes [.] // . ) | join( "" );

def node_to_xml:
   if type == "string" then
      text_to_xml
   else
      (
         if .attrs then
            .attrs |
            to_entries |
            map( " " + .key + "=\"" + ( .value | text_to_xml_attr_val ) + "\"" ) |
            join( "" )
         else
            ""
         end
      ) as $attrs |

      if .children and ( .children | length ) > 0 then
         ( .children | map( node_to_xml ) | join( "" ) ) as $children |
         "<" + .name + $attrs + ">" + $children + "</" + .name + ">"
      else
         "<" + .name + $attrs + "/>"
      end
   end
;

def fix_tree( $name ):
   type as $type |
   if $type == "array" then
      .[] | fix_tree( $name )
   elif $type == "object" then
      reduce to_entries[] as { key: $k, value: $v } (
         { name: $name, attrs: {}, children: [] };

         if $k[0:1] == $attr_prefix then
            .attrs[ $k[1:] ] = $v
         elif $k == $content_key then
            .children += [ $v ]
         else
            .children += [ $v | fix_tree( $k ) ]
         end
      )
   else
      { name: $name, attrs: {}, children: [ . ] }
   end
;

def fix_tree: fix_tree( "" ) | .children[];

fix_tree | node_to_xml

jqplay上的演示

它是使用

代码语言:javascript
运行
复制
jq -R 'above progam' file.json >file.xml

您还可以将程序放置在文件中(例如json_to_xml.jq),并使用以下方法:

代码语言:javascript
运行
复制
jq -Rf json_to_xml.jq file.json >file.xml

我走了两步路。我首先将输入转换为明确的格式,然后将结果转换为XML。这两个步骤可以合并。下面是提供的输入的第一次转换的结果:

代码语言:javascript
运行
复制
{
  "name": "security-settings",
  "attrs": {
    "xmlns": "urn:activemq:core"
  },
  "children": [
    {
      "name": "security-setting",
      "attrs": {
        "match": "#"
      },
      "children": [
        {
          "name": "permission",
          "attrs": {
            "type": "createNonDurableQueue",
            "roles": "admins"
          },
          "children": []
        },
        {
          "name": "permission",
          "attrs": {
            "type": "deleteNonDurableQueue",
            "roles": "admins"
          },
          "children": []
        },
        {
          "name": "permission",
          "attrs": {
            "type": "manage",
            "roles": "admins"
          },
          "children": []
        }
      ]
    }
  ]
}

请注意,转换原始XML的格式是有损的。例如,它失去了具有不同名称的XML元素的相对顺序。这意味着上述程序的输出可能在很大程度上不同于原始XML。但是,除非您使用一个不有损的JSON模式,否则这是无法逃避的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73038301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档