-
Notifications
You must be signed in to change notification settings - Fork 227
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
JSON to maintain array on predefined objects #544
Comments
Hey, to always produce arrays i think a schema or some new option would be needed (fq's xml decoder has an option to produce array tuples btw Also to properly convert/validate types etc a schema is needed i think, but you could do something like this but it might incorrectly convert string attributes or bodies. <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>
<name>BB</name>
<value>true</value>
</bitem>
<bitem>
<name>BB</name>
<value>[1,2,3]</value>
</bitem>
</root> $ fq 'walk(if type == "object" then with_entries(.value |= if type == "object" then [.] end) else (fromjson? | scalars) // . end)' test.xml
{
"root": [
{
"aitem": [
{
"name": "abc",
"value": 123
}
],
"bitem": [
{
"name": "bbbb",
"value": 2222
},
{
"name": "BB",
"value": 22
},
{
"name": "BB",
"value": true
},
{
"name": "BB",
"value": "[1,2,3]"
}
]
}
]
} A more correct solution i guess would be if the xml decoder supported xsd schema as a option somehow. Would it also validation in addition to convert types? Hope that helps and I can't give any promises that I will try to implement it, a bit busy with other fq and non-fq related things. Is it something you would like to look into? happy to give advice and review etc |
Hi again, did you get anywhere with this? don't think i will work on this anytime soon |
@wader , NO found any program to define data using schema Could you please help construct the json output from fq command to satisfy Below conditions.
|
Hi, with the above jq expression i get this, how close is that? could you give an example of expected json output? To do more complex things you would probably need to implement some kind of schema to describe the transformations, alternatively use a jq expression to describe the rewrites. {
"content": [
{
"country": "JAP",
"description": "ITEMS",
"exportDeclaration": [
{
"lineItems": [
{
"description": "PRODUCT A121",
"exportReasonType": "Permanent",
"itemCodes": [
{
"typeCode": "outbound",
"value": 82828282
}
],
"manufacturerCountry": "MY",
"number": 1,
"quantity": [
{
"unitOfMeasurement": "PCS",
"value": 1600
}
]
},
{
"description": "PRODUCT B121",
"exportReasonType": "Permanent",
"itemCodes": [
{
"typeCode": "outbound",
"value": 84848484
}
],
"manufacturerCountry": "MY",
"number": 2,
"quantity": [
{
"unitOfMeasurement": "PCS",
"value": 1800
}
]
}
],
"tracking": [
{
"date": "2023-02-28",
"number": "123456789"
}
]
}
]
}
]
} |
@wader , Would like to see some command in FQ , one command to convert XML to JSON and accordingly Mandate ARRAYS. Expected JSON Output
|
Is your expected JSON same as what i posted? looks very similar. That you can get by doing
|
@wader , Expected JSON has only normally arrays or mandated arrays in square braces.
|
Ok, sorry a bit busy with other things. But as it sounds like you want to do specific things based on key names etc so i would say jq itself is a good language to express those rules. Maybe try stackoverflow for a quicker answer and it's not really fq specific. |
@wader , Could you help in below JQ expression which works for single lineItems set ,
Sample JSON
|
Hi, try |
@wader , Opening discussion at right forum now.
Could you please provide a combined command to do both tasks at once
Expected Output , Only specified like .aitem, .oitem, .sitem must become array but not entire JSON & All numbers/floats/booleans are as non string without double quoted.
The text was updated successfully, but these errors were encountered: