Saya punya pertanyaan tentang menggabungkan objek JSON. Katakanlah kita memiliki dua titik akhir REST API. Seseorang mengembalikan JSON yang terlihat seperti ini:

[  
    {​​​​​​​​​​​​​
        "id": 999911090,
        "project_id": 74963091,
        "created_at": "2021-05-14T19:13:43.963Z",
        "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f"
    }​​​​​​​​​​​​​​​​​​​​​​​​​​​,
    {​​​​​​​​​​​​​​​​​​​​​​​​​​​
        "id": 999911091,
        "project_id": 74963092,
        "created_at": "2021-05-14T19:13:43.963Z",
        "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b"
    }​​​​​​​​​​​​​​​​​​​​​​​​​​​
]

Titik akhir kedua mengembalikan JSON yang berbeda tetapi sha akan sama dengan set pertama dan akan memiliki satu atau lebih elemen dengan sha yang diberikan:

[
    {​
        "id": 999999984,
        "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f",
        "status": "success",
        "created_at": "2021-04-23T17:54:30.836Z"
    }​,
    {​
        "id": 999999985,
        "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f",
        "status": "success",
        "created_at": "2021-04-23T17:55:30.836Z"
    }​,
    {​
        "id": 999999986,
        "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f",
        "status": "success",
        "created_at": "2021-04-23T17:56:30.836Z"
    }​,
    {​
        "id": 999999987,
        "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b",
        "status": "success",
        "created_at": "2021-04-23T17:57:30.836Z"
    }​,
    {​
        "id": 999999988,
        "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b",
        "status": "success",
        "created_at": "2021-04-23T17:58:30.836Z"
    }​,
    {​
        "id": 999999989, 
        "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b", 
        "status": "success", 
        "created_at": "2021-04-23T17:59:30.836Z",
    }​
]

Jadi pertanyaannya adalah: apakah mungkin untuk "bergabung" dengan dua objek JSON ini di sha yang sama?

Yang ingin saya akhiri adalah seperti ini:

[{
    "id": 999911090,
    "project_id": 74963091,
    "created_at": "2021-05-14T19:13:43.963Z",
    "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f",
    "results": [{
            "id": 999999984,
            "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f",
            "status": "success",
            "created_at": "2021-04-23T17:54:30.836Z"
        },
        {
            "id": 999999985,
            "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f",
            "status": "success",
            "created_at": "2021-04-23T17:55:30.836Z"
        },
        {
            "id": 999999986,
            "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f",
            "status": "success",
            "created_at": "2021-04-23T17:56:30.836Z"
        }
    ]
}, {
    "id": 999911091,
    "project_id": 74963092,
    "created_at": "2021-05-14T19:13:43.963Z",
    "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b",
    "results": [{
        "id": 999999987,
        "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b",
        "status": "success",
        "created_at": "2021-04-23T17:57:30.836Z"
    }, {
        "id": 999999988,
        "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b",
        "status": "success",
        "created_at": "2021-04-23T17:58:30.836Z"
    }, {
        "id": 999999989,
        "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b",
        "status": "success",
        "created_at": "2021-04-23T17:59:30.836Z"
    }]
}]
-1
Mystery Achievement 25 Mei 2021, 21:22

1 menjawab

Jawaban Terbaik

Meskipun pria itu belum melakukan upaya yang jujur, saya ingin membagikan solusi yang mungkin, kalau-kalau ada yang menganggap ini berguna (berasal dari Google):

left = [
    { "id": 999911090, "project_id": 74963091, "created_at": "2021-05-14T19:13:43.963Z", "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f", },
    { "id": 999911091, "project_id": 74963092, "created_at": "2021-05-14T19:13:43.963Z", "sha": "25e759f5be7e0e6d17eb6f46869322733296e34b", },
]
right = [
    { "id": 999999984, "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f", "status": "success", "created_at": "2021-04-23T17:54:30.836Z", },
    { "id": 999999985, "sha": "d4393d8fd0c4d124d54a79703ea2d0ea49ce8b7f", "status": "success", "created_at": "2021-04-23T17:55:30.836Z", },
]

# pip install convtools
from convtools import conversion as c

input_data = {"left_collection": left, "right_collection": right}
converter = (
    c.join(
        c.item("left_collection"),
        c.item("right_collection"),
        c.LEFT.item("sha") == c.RIGHT.item("sha"),
    )
    .pipe(
        c.group_by(
            c.item(0, "id"),
            c.item(0, "project_id"),
            c.item(0, "created_at"),
            c.item(0, "sha"),
        ).aggregate(
            {
                "id": c.item(0, "id"),
                "project_id": c.item(0, "project_id"),
                "created_at": c.item(0, "created_at"),
                "sha": c.item(0, "sha"),
                "results": c.ReduceFuncs.Array(c.item(1)),
            }
        )
    )
    # pass debug=True + install "black" to see the generated ad-hoc code
    .gen_converter()  
)
result = converter(input_data)

0
Nikita Almakov 25 Mei 2021, 19:19