Home > Net >  python convert JSON object contet type
python convert JSON object contet type

Time:12-08

I'm working on reading data stored in text and csv files using python and process them to create a new JSON document and all works fine except for the content type is application/octet-stream but when I create the JSON document I want this file content type to be application/json.

enter image description here

Data in json_document variable

[
   {
      "URL":"http://myexample.net",
      "domain":"abc",
      "domainSerialId":"skdksj12391ncjsacn",
      "proxy":"myexample.net"
   },
   {
      "URL":"http://myexample.org",
      "domain":"def",
      "domainSerialId":"sakdjsaye132978ejwdnwd",
      "proxy":"myexample.org"
   }
]

json.dumps(json_document)

This is the new consolidated JSON document that gets created. Having issue trying to convert the content type using json.dumps. I upload these documents to Azure blob storage and see the file Content Type to be different.

CodePudding user response:

Assuming you are using the Blob Service API, you need to specify the content-type in your request, otherwise the default is application/octet-stream.

https://learn.microsoft.com/en-us/rest/api/storageservices/put-blob

Edited:

Here is a code example using the Azure Python SDK

# Instantiate a BlobServiceClient using a connection string
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(self.connection_string)

# Instantiate a ContainerClient
container_client = blob_service_client.get_container_client("mynewcontainer")

# Set mime-type and upload file
content_settings = ContentSettings(content_type='application/json')
container_client.upload_blob(name=dest_path, data=contents.encode('utf-8'),overwrite=True, content_settings=content_settings)
  • Related