Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate array always for specified xml element #2514

Closed
ajit4sbi opened this issue Dec 28, 2022 · 5 comments
Closed

Generate array always for specified xml element #2514

ajit4sbi opened this issue Dec 28, 2022 · 5 comments
Labels

Comments

@ajit4sbi
Copy link

  • Generate array always for specified xml element "aitem" even with single set .
  • It must open square braces as an array like aitem[{}]
  • Please also specify expression to write in /.jq file as a def, to maintain many single set items to be an array.
<root>
    <aitem>
        <name>abc</name>
        <value>123</value>
    </aitem>
    <bitem>
        <name>bbbb</name>
        <value>2222</value>
    </bitem>
    <bitem>
        <name>BB</name>
        <value>22</value>
    </bitem>
</root>

Normal Output aitem{} bitem[{}{}]

 {
  "root": {
    "aitem": {
      "name": "abc",
      "value": "123"
    },
    "bitem": [
      {
        "name": "bbbb",
        "value": "2222"
      },
      {
        "name": "BB",
        "value": "22"
      }
    ]
  }
}

Expected Output , aitem[{}] bitem[{}{}] even its single set aitem must become array

{
  "root": {
    "aitem": [
    {
      "name": "abc",
      "value": "123"
    }
    ],
    "bitem": [
      {
        "name": "bbbb",
        "value": "2222"
      },
      {
        "name": "BB",
        "value": "22"
      }
    ]
  }
 }
@wader
Copy link
Member

wader commented Dec 28, 2022

Hi, seems unrelated to jq. Did you mean to open an issue for https://github.com/kislyuk/yq, https://github.com/mikefarah/yq or https://github.com/wader/fq etc? but maybe you can post-process the JSON, something like walk(if type == "object" then with_entries(.value |= if type == "object" then [.] end) end) produces:

{
  "root": [
    {
      "aitem": [
        {
          "name": "abc",
          "value": "123"
        }
      ],
      "bitem": [
        {
          "name": "bbbb",
          "value": "2222"
        },
        {
          "name": "BB",
          "value": "22"
        }
      ]
    }
  ]
}

@ajit4sbi
Copy link
Author

@wader ,Thanks for your attention

yes i tried converting xml to json with yq first, helpful if you can provide solution for same within YQ

I am trying to process with JQ for proper json

Can we do xml>json conversion & serialize json with FQ ,

Do any of the YQ, JQ, FQ packages do conversion & serialize output based on XML or JSON schema

Please correct my command or how can i use same in definition file

jq 'walk(if type == "object" then with_entries(.value |= if type == "object" then [.] end) end)' test.json

@wader
Copy link
Member

wader commented Dec 28, 2022

Not sure about yq, and not sure which one of the two named yq your using. But with fq you can do:

$ fq 'walk(if type == "object" then with_entries(.value |= if type == "object" then [.] end) end)' test.xml
{
  "root": [
    {
      "aitem": [
        {
          "name": "abc",
          "value": "123"
        }
      ],
      "bitem": [
        {
          "name": "bbbb",
          "value": "2222"
        },
        {
          "name": "BB",
          "value": "22"
        }
      ]
    }
  ]
}
# and you can manipulate the json and get xml back as the values follow the xml<->json mapping rules
$ fq -r '.root.bitem += [{hello: 123}] | to_xml({indent:2})' test.xml
<root>
  <aitem>
    <name>abc</name>
    <value>123</value>
  </aitem>
  <bitem>
    <name>bbbb</name>
    <value>2222</value>
  </bitem>
  <bitem>
    <name>BB</name>
    <value>22</value>
  </bitem>
  <bitem>
    <hello>123</hello>
  </bitem>
</root>

@ajit4sbi
Copy link
Author

ajit4sbi commented Dec 29, 2022

@wader

Working with FQ now, Any idea can i create xsd schema files & ask FQ to do accordingly as per schema file.

Requirement as below , would need two tasks doing in single command

  • I would like to only specify ".aitem, .oitem, .sitem" like single set objects to be an array, but not all others
  • convert all numbers/floats/booleans to be without double quoted

@wader
Copy link
Member

wader commented Dec 29, 2022

fq does not currently support decoding to json based on xsd, don't know if one of the yq:s does. It's not something i plan on adding myself at the moment, but feel free to work on it.

@itchyny itchyny added the support label Jun 3, 2023
@itchyny itchyny closed this as completed Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants