1

I am facing problem in merging two json strings. Code goes like this:

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

The network_data will be having json string as:[one who doesn't have vnstat consider this as network_data value ]

{"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}]}}]}

Here I want to merge json_input and network_data which should be again a json. I tried this for merging:

network_data.update(json_input)

But throwing error as:

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

Thanks in advance!!

1
  • Easiest way to handle AttributeError is to indeed confirm that the object has that attribute by using the getattr built-in to check. Commented Oct 9, 2017 at 16:39

1 Answer 1

3

The network_data variable seems to be a string. You have to convert it into a dict, merge the dicts, and then convert it to json.

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

For older versions of python

network_dict = json.loads(network_data)
network_dict.update(input)
network_dict = json.dumps(new_dict)
Sign up to request clarification or add additional context in comments.

6 Comments

new_dict = {**jso_nop, **json_network_data} ^ SyntaxError: invalid syntax Note:using python 2.7!
For network_dict = json.loads(network_data) getting Error as ` File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer` @blue_note: Help me!!
@Sjn73: This is contradictory. This error is caused because network_data does not seem to be a string. But, based on the error you mention in your question, it is a string. Check it first (print(type(network_data)))
@blue_data: output for print(type(network_data)) <type 'dict'>
@Sjn73: yes, what does it print?
|

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.