dosubot[bot] commented on issue #29871:
URL: https://github.com/apache/superset/issues/29871#issuecomment-2270673387

   <!-- Answer -->
   To resolve the error **{'message': {'json_metadata': [{'positions': ['Not a 
valid mapping type.']}]}}**, you need to ensure that the `positions` key in the 
`json_metadata` is correctly formatted as a valid mapping type (i.e., a 
dictionary).
   
   Here's how you can modify your code to ensure the `positions` key is 
correctly formatted:
   
   1. Fetch the `position_json` from the dashboard data.
   2. Parse the `position_json` to ensure it is a valid dictionary.
   3. Add the parsed `position_json` to the `json_metadata`.
   
   Here's the updated code:
   
   ```python
   import json
   import requests
   
   dashboard_id = 1
   headers = {
       "Content-Type": "application/json",
       "Authorization": f"Bearer {self.access_token}",
       "X-CSRFToken": self.csrf_token
   }
   
   # Fetch the dashboard data
   dashboard_response = 
requests.get(f"{superset_url}/api/v1/dashboard/{dashboard_id}", 
                                     headers=headers, cookies=self.cookies)
   dashboard_data = dashboard_response.json()["result"]
   
   # Extract and update json_metadata
   json_meta = dashboard_data.get("json_metadata", "")
   json_meta = json.loads(json_meta)
   
   # Ensure positions is a valid mapping type
   positions = dashboard_data.get("position_json", "{}")
   positions = json.loads(positions) if isinstance(positions, str) else 
positions
   json_meta["positions"] = positions
   
   # Prepare the request payload
   request_payload = {
       "dashboard_title": "intergrate_test",
       "duplicate_slices": True,
       "json_metadata": json.dumps(json_meta)
   }
   
   # Make the copy API call
   r = requests.post(f'{superset_url}/api/v1/dashboard/{dashboard_id}/copy/', 
                     json=request_payload, headers=headers, 
cookies=self.cookies)
   print(r.json())
   ```
   
   This code ensures that the `positions` key is included in the 
`json_metadata` as a valid dictionary, which should resolve the error 
<sup>[[1]](https://github.com/apache/superset/blob/master/superset-frontend/src/dashboard/actions/dashboardState.js)</sup><sup>[[2]](https://github.com/apache/superset/blob/master/tests/integration_tests/dashboards/api_tests.py)</sup><sup>[[3]](https://github.com/apache/superset/blob/master/tests/integration_tests/dashboards/dao_tests.py)</sup>.
   
   
   <!-- Dosu Comment Footer -->
   *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to