day 11 - json_util
This commit is contained in:
parent
0636999ac9
commit
00d595507d
1 changed files with 34 additions and 0 deletions
|
@ -0,0 +1,34 @@
|
|||
---
|
||||
layout: post
|
||||
title: "day 11"
|
||||
date: 2018-01-17
|
||||
categories: programming
|
||||
---
|
||||
|
||||
# 100 Days of Code
|
||||
|
||||
### Day 11:
|
||||
Took a break on the weekend, but I am back at it. Today, I made a class for
|
||||
decoding json data. Here is the code. I can use this with my xonsh shell now so
|
||||
its proving to be useful.
|
||||
|
||||
```python
|
||||
|
||||
import os
|
||||
import json
|
||||
from pprint import pprint
|
||||
|
||||
class JsonUtils(object):
|
||||
""" Json utility library """
|
||||
|
||||
|
||||
def decode(self, json_file):
|
||||
""" Decode a json string or file that contains json """
|
||||
if os.path.isfile(json_file):
|
||||
with open(json_file, 'r'):
|
||||
data = json.load(json_file)
|
||||
return pprint(data)
|
||||
else:
|
||||
data = json.loads(json_file)
|
||||
return pprint(data)
|
||||
```
|
Loading…
Add table
Reference in a new issue