0

I am newbie in Python. I have some difficulties generating a nested JSON using for loop in python. For generating a nested JSON, I got the length of dictionary on runtime and based on the dictionary length I want to generate nested JSON. eg. I got the length of dictionary is 4. The dictionary length may vary. Here is my data_dict dictionary:

data_dict = {"PHOTO_1" : {"key1" : "PHOTO_2", "key2" : "PHOTO_3", "key3" : "PHOTO_4"}, "PHOTO_2" : {"key1" : "PHOTO_1", "key2" : "PHOTO_3"},"PHOTO_3" : {"key1" : "PHOTO_2"},"PHOTO_4" : {"key1" : "PHOTO_1", "key2" : "PHOTO_2", "key3" : "PHOTO_3"}}

Expected result :

{
    "Requests": [
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_1"
                },
                "connections": {
                    "target": {
                        "id": "PHOTO_2"
                    }
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_1"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_3"
                    }
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_1"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_4"
                    }
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_2"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_1"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_2"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_3"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_3"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_2"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_4"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_1"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_4"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_2"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_4"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_3"
                    },
                }
            },
            "updateData": "connections"
        }
    ]
}

Please help. I'm not getting how to solve this query? Please don't mark it duplicate. I have already checked all the answers and my JSON query is totally different.

7
  • Possible duplicate of Generating json in python for app engine Commented May 30, 2017 at 6:26
  • has the dictionary always length 3? Commented May 30, 2017 at 6:31
  • @RomanPerekhrest No, the dictionary length has not fixed. It is vary. Commented May 30, 2017 at 6:36
  • your json is invalid, you can't have same target keys Commented May 30, 2017 at 7:24
  • @ArunaRajput, you got my solution Commented May 30, 2017 at 9:48

1 Answer 1

1

The solution using itertools.permutations() function:

import itertools, json

data_dict = {"first_photo" : "PHOTO_1", "second_photo" : "PHOTO_2", "Thrid" : "PHOTO_3"}
result = {"Requests":[]}

for pair in sorted(itertools.permutations(data_dict.values(), 2)):
    result["Requests"].append({"photo":{"photoId":{"id": pair[0]},
                                        "connections":{"target":{"id": pair[1]}}},"updateData": "connections"})

print(json.dumps(result, indent=4))

The additional approach for the new input dict:

data_dict = {"PHOTO_1" : {"key1" : "PHOTO_2", "key2" : "PHOTO_3", "key3" : "PHOTO_4"}, "PHOTO_2" : {"key1" : "PHOTO_1", "key2" : "PHOTO_3"},"PHOTO_3" : {"key1" : "PHOTO_2"},"PHOTO_4" : {"key1" : "PHOTO_1", "key2" : "PHOTO_2", "key3" : "PHOTO_3"}}
result = {"Requests":[]}

for k,d in sorted(data_dict.items()):
    for v in sorted(d.values()):
        result["Requests"].append({"photo":{"photoId":{"id": k},
                                        "connections":{"target":{"id": v}}},"updateData": "connections"})

print(json.dumps(result, indent=4))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.