2017-10-09 15 views
1

2つのjson文字列をマージする際に問題があります。 コードは次のようになります:2つのjson文字列をマージする

input = {'ldb': 25, 'baseB': 4, 'code': 100, 'cache-6': 55, 'Xauthority': 1, 'baseA': 4, 'npmrc': 1, 'apmrc': 1, 'gz': 176, 'dbf': 1, 'lst': 2, 'jsonlz4': 39, 'ps1': 8, 'vsixmanifest': 4, 'pkl': 5, 'gitignore': 8, 'xml': 44, 'watchr': 1, '22': 1, 'la': 9, 'sublime-keymap': 1, 'cache': 103, 'jar': 1, 'ts': 704, 'desktop': 3, 'source': 1, 'sqlite3-journal': 1, 'TAG': 1, '4': 1, 'usage': 6, 'oTN4k5': 1, 'bin': 7, 'docx': 9, '7': 9, 'md': 844, 'sth': 38, 'woff': 3, 'dbt': 1, 'V2XO5Y': 1, 'dat': 3, 'fingerprint': 5, 'lz4': 1, 'converted-launchers': 1, 'bat': 5, 'bau': 1, 'png': 2314, 'name': 2, 'crt': 8, 'h': 122, '56': 3, 'list': 2, 'dll': 42, 'patch': 2, 'zsh-update': 1, 'editorconfig': 4, 'stderr': 8, 'sublime_session': 1, 'xls': 1, 'bak': 1, 'bond': 108, 'profile': 1, 'cmd': 2, 'old': 26, 'babelrc': 2, 'locale': 1, 'cfg': 3, 'htm': 1, 'odt': 1, 'pyc': 51, 'xlb': 2, 'gypi': 1, 'md~': 1, 'sqlite-journal': 1, 'odb': 2, 'dic': 1, 'todo': 1, 'tvc': 1, 'out': 20, 'tmp': 4, 'stats': 2, 'icc': 1, 'zprofile': 1, 'ttf': 109, 're': 21, '3': 1, 'rb': 1, 'ics': 3, 'bash_history': 1, 'reg': 3, 'metadata': 6, 'dirs': 1, 'run': 1, 'asar': 1, 'tdb': 3, 'journal': 2, 'zshrc': 1, 'xpi': 9, 'pub': 1, 'js': 4336, 'asc': 1, '0': 11, 'base': 1, 'coffee': 8, 'key': 12, 'css': 112, 'less': 1, 'xbel': 1, 'properties': 2, 'bash': 1, 'lock': 3, 'eslintignore': 1, 'c': 283, 'swo': 1, 'idx': 5, 'lsup7I': 1, 'jshintrc': 6, 'un~': 2, 'vbs': 1, 'rdf': 2, 'dmrc': 1, 'Mdg80A': 1, 'sbstore': 42, 'pdf': 18, 'tmLanguage': 11, 'xlc': 2, 'markdown': 10, 'final': 1, 'tgz': 2, 'iml': 2, 'xz': 1, 'bashrc': 1, 'db-journal': 3, 'pf2': 5, 'localstorage': 94, 'tmPreferences': 4, 'isrunning': 1, 'txt': 287, 'orig': 2, 'gvdb': 1, 'little': 2, 'git': 1, 'gitmodules': 1, 'log': 47, 'zip': 6, 'yaml': 8, 'stamp': 2, 'vxd': 1, 'fmt': 1, 'py': 480, 'sock': 2, 'pb': 32, 'gif': 3, 'json': 2130, '2': 2, 'js-20170612122310': 1, '6': 1, 'swp': 2, 'bash_logout': 1, 'sqlite-shm': 4, 'pl': 1, 'gpg-agent': 1, 'scss': 2, 'sdv': 1, 'parentlock': 2, 'cson': 8, 'rcache': 2, 'otf': 29, 'xinputrc': 1, 'gitconfig': 1, 'npmignore': 84, 'localstorage-journal': 94, 'gyp': 1, 'rst': 11, '1vCfy0': 1, 'jshintignore': 1, 'metadata-v2': 6, 'bnf': 1, 'x86_64-pc-linux-gnu': 1, 'exc': 1, 'info': 1, 'zcompdump': 1, 'js-20170816210634': 1, 'sublime-snippet': 14, 'tsv': 1, 'gpg': 1, 'update-timestamp': 1, 'mk': 5, 'spec': 9, 'sqlite-wal': 4, 'ICEauthority': 1, 'pem': 4, 'deb': 3, 'zsh': 235, 'pack': 5, 'zsh_history': 1, 'sqlite': 24, 'tar': 1, 'stdout': 8, 'jscsrc': 1, 'prl': 9, 'pro': 1, 'gitkeep': 2, 'sublime-settings': 1, 'jpg': 23, 'jamignore': 1, 'sample': 72, 'ino': 1, 'pset': 42, 'ini': 14, 'conf': 17, 'xcu': 1, 'gradle': 3, 'plist': 1, 'xsession-errors': 1, 'keystore': 1, 'nls': 1, 'sudo_as_admin_successful': 1, 'bdic': 1, 'yml': 47, '1': 8, 'html': 89, '5': 10, 'MZPZ5Y': 1, 'sqlite3': 1, 'pak': 61, 'config': 1, 'mozlz4': 2, 'node': 16, 'map': 84, 'whl': 2, 'db': 17, 'zsh-template': 1, 'zsh-theme': 143, 'trashinfo': 3, 'sys': 1, 'm': 1, 'sublime-build': 1, 'pym': 16, 'ico': 1, 'php': 12, 'B5S94Y': 1, 'cc': 81, 'viminfo': 1, 'a': 2, 'exe': 21, 'db-shm': 1, 'java': 7, 'svg': 187, 'eslintrc': 6, 'keyring': 1, 'DB': 4, 'JPG': 1, 'thm': 1, 'sh': 41, 'so': 18, 'eot': 1, 'kbx': 1, 'xba': 1, 'scpt': 2, 'opts': 11, 'db-wal': 1} 
json_input=json.dumps(input,ensure_ascii=False)#converted dictionary to json 
network_data = subprocess.check_output("vnstat --json", shell=True)#output will be json for this execution requires vnstat 

ようnetwork_dataは、JSON文字列を持つことになります。私はjson_inputをマージする[network_data値としてこれを考慮vnstat持っていない1]

ここ
{"vnstatversion":"1.14","jsonversion":"1","interfaces":[{"id":"wlp2s0","nick":"wlp2s0","created":{"date":{"year":2017,"month":10,"day":6}},"updated":{"date":{"year":2017,"month":10,"day":9},"time":{"hour":21,"minutes":20}},"traffic":{"total":{"rx":828111,"tx":202432},"days":[{"id":0,"date":{"year":2017,"month":10,"day":9},"rx":487055,"tx":28163},{"id":1,"date":{"year":2017,"month":10,"day":8},"rx":340996,"tx":174242},{"id":2,"date":{"year":2017,"month":10,"day":6},"rx":60,"tx":27}],"months":[{"id":0,"date":{"year":2017,"month":10},"rx":828111,"tx":202432}],"tops":[{"id":0,"date":{"year":2017,"month":10,"day":8},"time":{"hour":19,"minutes":39},"rx":340996,"tx":174242},{"id":1,"date":{"year":2017,"month":10,"day":6},"time":{"hour":15,"minutes":58},"rx":60,"tx":27}],"hours":[{"id":0,"date":{"year":2017,"month":10,"day":9},"rx":207262,"tx":9886},{"id":1,"date":{"year":2017,"month":10,"day":9},"rx":206393,"tx":9457},{"id":2,"date":{"year":2017,"month":10,"day":9},"rx":44775,"tx":4989},{"id":11,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":12,"date":{"year":2017,"month":10,"day":9},"rx":25669,"tx":1543},{"id":13,"date":{"year":2017,"month":10,"day":9},"rx":1983,"tx":1859},{"id":14,"date":{"year":2017,"month":10,"day":9},"rx":83,"tx":104},{"id":21,"date":{"year":2017,"month":10,"day":9},"rx":890,"tx":325},{"id":22,"date":{"year":2017,"month":10,"day":8},"rx":30989,"tx":75505},{"id":23,"date":{"year":2017,"month":10,"day":8},"rx":13561,"tx":3119}]}},{"id":"enp3s0f1","nick":"enp3s0f1","created":{"date":{"year":2017,"month":10,"day":6}},"updated":{"date":{"year":2017,"month":10,"day":9},"time":{"hour":21,"minutes":20}},"traffic":{"total":{"rx":0,"tx":0},"days":[{"id":0,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":1,"date":{"year":2017,"month":10,"day":8},"rx":0,"tx":0},{"id":2,"date":{"year":2017,"month":10,"day":6},"rx":0,"tx":0}],"months":[{"id":0,"date":{"year":2017,"month":10},"rx":0,"tx":0}],"tops":[],"hours":[{"id":0,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":1,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":2,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":11,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":12,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":13,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":14,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":21,"date":{"year":2017,"month":10,"day":9},"rx":0,"tx":0},{"id":22,"date":{"year":2017,"month":10,"day":8},"rx":0,"tx":0},{"id":23,"date":{"year":2017,"month":10,"day":8},"rx":0,"tx":0}]}}]} 

network_dataこれは再びjsonでなければなりません。 私はマージのためにこれを試してみました:

network_data.update(json_input) 

しかしとしてエラーを投げ:事前に

network_data.update(json_input) 
AttributeError: 'str' object has no attribute 'update' 

感謝を!

+0

AttributeError'が実際にオブジェクトをチェックするために、組み込みの 'getattr'を使用して、その属性を持っていることを確認することです'処理するための最も簡単な方法の古いバージョンの

network_dict = json.loads(network_data) new_dict = {**input, **network_dict} network_dict = json.dumps(new_dict) 

。 – smac89

答えて

3

network_data変数は文字列のようです。あなたはそれをdictに変換し、dictsをマージして、それをjsonに変換しなければなりません。パイソン

network_dict = json.loads(network_data) 
network_dict.update(input) 
network_dict = json.dumps(new_dict) 
+0

'new_dict = {** jso_nop、** json_network_data} ^ SyntaxError:無効な構文' 注:python 2.7を使用しています! – Sjn73

+0

@ Sjn73:編集された回答。 'network_dictについては –

+0

= json.loads ファイル "/usr/lib/python2.7/json/__init__.py" で(network_data) 'ファイル "" として ' 取得エラー、ライン1、ライン339 、ロード中 戻り値_default_decoder.decode ファイル "/usr/lib/python2.7/json/decoder.py"、行364、デコード中 obj、終了= self.raw_decode(s、idx = _w() s、0).end()) TypeError:予想される文字列またはバッファー @blue_note:私を助けてください! – Sjn73

関連する問題