How to prevent the following to show leading `@`?

```bash
xq-python . licenses.xml
```
```json
{
"licenses": {
"license": [
{
"@id": "a",
"@name": "alpha"
},
{
"@id": "b",
"@name": "beta"
}
]
}
}
```
Aug 5, 1:52pm

The leading @ in the JSON output (e.g., "@id", "@name") is a convention used by xq (and some other XML-to-JSON converters) to represent XML attributes in the JSON output. This is because XML attributes (like id and name in your example) are distinct from XML elements, and the @ prefix helps distinguish them in the JSON structure.


How to Remove the @ Prefix

Option 1: Use xq with --no-xml-attributes

If your version of xq supports it, you can use the --no-xml-attributes flag to omit the @ prefix for attributes. Try:

bash
xq-python --no-xml-attributes . licenses.xml

If this flag is not available, proceed to the next options.


Option 2: Post-Process with jq

Use jq to rename the keys and remove the @ prefix. For example:

bash
xq-python . licenses.xml | jq 'walk(if type == "object" then with_entries(.key |= sub("@"; "")) else . end)'

Explanation:

  • walk recursively traverses the JSON structure.
  • with_entries(.key |= sub("@"; "")) removes the @ prefix from all keys.

Option 3: Use a Custom Script

If you need more control, write a small script (e.g., in Python) to process the JSON and strip the @ prefix from keys. Example:

python
import json
import sys

def remove_at_prefix(obj):
    if isinstance(obj, dict):
        return {k.lstrip("@"): remove_at_prefix(v) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [remove_at_prefix(item) for item in obj]
    else:
        return obj

data = json.load(sys.stdin)
print(json.dumps(remove_at_prefix(data), indent=2))

Save this as remove_at.py and run:

bash
xq-python . licenses.xml | python3 remove_at.py

Result

The output will look like this:

json
{
  "licenses": {
    "license": [
      {
        "id": "a",
        "name": "alpha"
      },
      {
        "id": "b",
        "name": "beta"
      }
    ]
  }
}
Aug 5, 1:52pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more