How to parse json in bash or pipe curl output to python script

I'm looking to find some way to have a pretty printable curl output in json. I wrote a short python script for this, but it won't work with pipe Also I don't want to use subprocesses and run curl from them:

So python:

#!/usr/bin/python

import simplejson
from pprint import pprint
import sys
print pprint(simplejson.loads(sys.argv[1]))

      

And the json information:

{"response": {"utilization": {"eic": [{"date": "2012.03.06", "usage": []}, {"date": "2012.03.07", "usage": [{"srvCode": "SVC302", "upload": 267547188, "api-calls": {"fileGetInfo": 30, "getUserStorageQuota": 0, "setUserStorageQuota": 0, "fileUploadFlashInit": 31, "getAPISessionUser": 0, "setFileAccessControl": 0, "fileGetPreviewUrl": 0, "fileStartMultipartUpload": 0, "getServiceQuota": 0, "fileGetPreviewUrlsForBunch": 10, "xcodingGetStreamUrl": 0, "getSessionTimeLimit": 0, "fileGetCoversUrlsForBunch": 27, "makePreviews": 0, "setServiceQuota": 0, "getAPISessionTrusted": 3, "getFileAccessControl": 0, "xcodingGetFormats": 0, "getQuotaNotificationEmail": 0, "fileGetDownloadUrl": 0, "xcodingGetStreamInfo": 0, "fileUploadDone": 30, "getLocalServiceUtilization": 9, "getServiceUtilization": 0, "fileDelete": 19, "setSessionTimeLimit": 0, "fileGetMultipartUploadUrl": 0, "fileUploadInit": 0, "extractFileMetadata": 30, "setQuotaNotificationEmail": 0}, "average-storage": 3801210959.4961309, "download": 0, "transcoding": 0}]}]}}}

      

+3


source to share


2 answers


Usage json.tool

from shell to check and print:



$ echo '{"json":"obj"}' | python -mjson.tool
{
    "json": "obj"
}

      

+7


source


You are trying to read the first argument passed to the program, which is incorrect.



When using pipes, the stream is redirected to stdin programs. You should read from sys.stdin, see " How do you read from stdin in python ".

+3


source







All Articles