2

I have two json files here, both have the same content only in a different order, and these are to be checked for equality with a diff.

I already sort the keys with jq -S, but now I have to make sure that the strings are sorted equally within the arrays.

Unfortunately, I fail at the moment, I am not quite clear how I get to the right level and how I can sort the content.

Here is an example structure of the jsons, the array 'allowed-test-mapper-data' should be sorted in descending order

{
    "accessCodeLife": 60,
    "accessCodeLifespan": 1800,
    "accessCodeType": 300,
    "components": {
        "test.data.app": [
        {
        "config": {
            "allow-default-test-scopes": [
                "true"
            ]
        },
        "name": "Allowed Test Client",
        "id": "allowed-testdata",
        "subComponents": {},
        "subType": "testdata"
        },
        {
        "config": {
            "allowed-test-mapper-data": [
                "alfred",
                "usa",
                "canada",
                "somedata",
                "alcohol",
                "brother"
                ]   
            }
        }
        ]
    }
}

Can someone help me here ? Would be great :)

1 Answer 1

3

Use the update assignment |= operator to change a part of the structure:

jq  '.components."test.data.app"[].config."allowed-test-mapper-data"
         |= if . then sort else empty end' file.json
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot :) that is what i need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.