Use the json module to produce JSON output:
import json
with open(outputfilename, 'wb') as outfile:
json.dump(row, outfile)
This writes the JSON result directly to the file (replacing any previous content if the file already existed).
If you need the JSON result string in Python itself, use json.dumps() (added s, for 'string'):
json_string = json.dumps(row)
The L is just Python syntax for a long integer value; the json library knows how to handle those values, no L will be written.
Demo string output:
>>> import json
>>> row = [1L,[0.1,0.2],[[1234L,1],[134L,2]]]
>>> json.dumps(row)
'[1, [0.1, 0.2], [[1234, 1], [134, 2]]]'
Answer from Martijn Pieters on Stack OverflowUse the json module to produce JSON output:
import json
with open(outputfilename, 'wb') as outfile:
json.dump(row, outfile)
This writes the JSON result directly to the file (replacing any previous content if the file already existed).
If you need the JSON result string in Python itself, use json.dumps() (added s, for 'string'):
json_string = json.dumps(row)
The L is just Python syntax for a long integer value; the json library knows how to handle those values, no L will be written.
Demo string output:
>>> import json
>>> row = [1L,[0.1,0.2],[[1234L,1],[134L,2]]]
>>> json.dumps(row)
'[1, [0.1, 0.2], [[1234, 1], [134, 2]]]'
import json
row = [1L,[0.1,0.2],[[1234L,1],[134L,2]]]
row_json = json.dumps(row)
When you load the json you are actually converting it into a python dictionary. You can then simply show all values:
inputJSON = open('input.json')
inputData = json.load(inputJSON)
values = list(inputData.values())
If key:value is in plain text, you can do:
values = [pair.split(":")[1] for pair in inputData]
If by key:value you mean a dict type {key: value}, you can do as follows:
values = [list(pair.values())[0] for pair in inputData]
Unable to append data to Json array object with desired output
Creating JSON Array of data with python
python - How to convert Json to array? - Stack Overflow
python - Read in JSON into array - Stack Overflow
import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
print(data['fruits'])
# the print displays:
# ['apple', 'banana', 'orange']
You had everything you needed. data will be a dict, and data['fruits'] will be a list
Tested on Ideone.
import json
array = '{"fruits": ["apple", "banana", "orange"]}'
data = json.loads(array)
fruits_list = data['fruits']
print fruits_list
I think you need data into this format
[{'Financial Ratios': 'Ratios',
'Unnamed: 1': 'Formula ',
'Unnamed: 2': '2013',
'Unnamed: 3': 2012.0,
'Unnamed: 4': 'Point',
'Unnamed: 5': 'Max Score for the Ratio',
'Unnamed: 6': 'Weighted Score'},
{'Financial Ratios': 'Activity Ratios',
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': None,
'Unnamed: 5': None,
'Unnamed: 6': None},
{'Financial Ratios': 'Total Asset Turnover Ratio',
'Unnamed: 1': ' Sales/Total Assets (S/TA) (X5)',
'Unnamed: 2': 0.3688805385,
'Unnamed: 3': 0.46659987,
'Unnamed: 4': 1,
'Unnamed: 5': 50,
'Unnamed: 6': 10},
{'Financial Ratios': 'Working Capital Turnover',
'Unnamed: 1': 'Sales/Working Capital',
'Unnamed: 2': 1.5217292178,
'Unnamed: 3': 2.3187325549,
'Unnamed: 4': 0,
'Unnamed: 5': 15,
'Unnamed: 6': 0},
{'Financial Ratios': 'RiskCalc. v4. Activity Ratio 2',
'Unnamed: 1': 'Account Payable/Sales',
'Unnamed: 2': 0.3654207703,
'Unnamed: 3': 0.7809577867,
'Unnamed: 4': 1,
'Unnamed: 5': 10,
'Unnamed: 6': 10},
{'Financial Ratios': 'Days of Inventory on Hand (DOH)',
'Unnamed: 1': '365/(Cost of Goods Sold/Inventory)',
'Unnamed: 2': 126.7687160825,
'Unnamed: 3': 89.3996349298,
'Unnamed: 4': 0,
'Unnamed: 5': 5,
'Unnamed: 6': 0},
{'Financial Ratios': 'Days of Sales Outstanding (DSO)',
'Unnamed: 1': '365/(Sales/Trade Receivables)',
'Unnamed: 2': 201.1519182976,
'Unnamed: 3': 136.5175896751,
'Unnamed: 4': 0,
'Unnamed: 5': 5,
'Unnamed: 6': 0},
{'Financial Ratios': 'Number of Days of Payables',
'Unnamed: 1': '365/(Cost of Goods Sold/Trade Payables)',
'Unnamed: 2': 156.6392091208,
'Unnamed: 3': 343.2088569019,
'Unnamed: 4': 0,
'Unnamed: 5': 5,
'Unnamed: 6': 0},
{'Financial Ratios': 'Cash Conversion Cycle',
'Unnamed: 1': 'DOH + DSO – Number of Days of Payables',
'Unnamed: 2': 171.2814252594,
'Unnamed: 3': -117.291632297,
'Unnamed: 4': 0,
'Unnamed: 5': 10,
'Unnamed: 6': 0},
{'Financial Ratios': None,
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': 'Efficiency Score',
'Unnamed: 5': '100/',
'Unnamed: 6': 20},
{'Financial Ratios': 'Liquidity Ratios',
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': None,
'Unnamed: 5': None,
'Unnamed: 6': None},
{'Financial Ratios': 'liquidity',
'Unnamed: 1': 'X1, Working Capital/Total Assets (WC/TA)',
'Unnamed: 2': 0.24240879,
'Unnamed: 3': 0.2012305684,
'Unnamed: 4': 2,
'Unnamed: 5': 20,
'Unnamed: 6': 8},
{'Financial Ratios': 'Current Ratio',
'Unnamed: 1': 'Current Assets/Current Liabilities',
'Unnamed: 2': 1.4011830177,
'Unnamed: 3': 1.3122086769,
'Unnamed: 4': 3,
'Unnamed: 5': 40,
'Unnamed: 6': 24},
{'Financial Ratios': 'Quick Ratio',
'Unnamed: 1': '(Current Assets-Inventory)/Current Liabilities',
'Unnamed: 2': 1.2206383114,
'Unnamed: 3': 1.1649433701,
'Unnamed: 4': 4,
'Unnamed: 5': 30,
'Unnamed: 6': 24},
{'Financial Ratios': 'Cash Ratio',
'Unnamed: 1': 'Cash and Marketable Securities/Current Liabilities',
'Unnamed: 2': 0.1198051849,
'Unnamed: 3': 0.3207784969,
'Unnamed: 4': 0,
'Unnamed: 5': 10,
'Unnamed: 6': 0},
{'Financial Ratios': None,
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': 'liquidity Score',
'Unnamed: 5': '100/',
'Unnamed: 6': 56},
{'Financial Ratios': 'Solvency Ratios',
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': None,
'Unnamed: 5': None,
'Unnamed: 6': None},
{'Financial Ratios': 'Solvency',
'Unnamed: 1': 'X4, Equity/ TotalLiabilities (E/TL)',
'Unnamed: 2': 0.2542669835,
'Unnamed: 3': 0.3698847524,
'Unnamed: 4': 0,
'Unnamed: 5': 10,
'Unnamed: 6': 0},
{'Financial Ratios': 'Total Debt Ratio',
'Unnamed: 1': 'Total Debt/Total Assets',
'Unnamed: 2': 0.7972784209,
'Unnamed: 3': 0.7299884156,
'Unnamed: 4': 2,
'Unnamed: 5': 25,
'Unnamed: 6': 10},
{'Financial Ratios': 'Financial Liabilities Percentage',
'Unnamed: 1': 'short-term Financial Liabilities/Current Liabilities',
'Unnamed: 2': 0.1830886471,
'Unnamed: 3': 0.1413598101,
'Unnamed: 4': 0,
'Unnamed: 5': 10,
'Unnamed: 6': 0},
{'Financial Ratios': 'Current Liabilities to Net Worth',
'Unnamed: 1': '(Current Liabilities/Net Worth)*100',
'Unnamed: 2': 298.0614724129,
'Unnamed: 3': 238.7077877629,
'Unnamed: 4': 2,
'Unnamed: 5': 25,
'Unnamed: 6': 10},
{'Financial Ratios': 'Financial Leverage Ratio',
'Unnamed: 1': 'Total Assets/TotalEquity',
'Unnamed: 2': 4.9328739664,
'Unnamed: 3': 3.7035448029,
'Unnamed: 4': 0,
'Unnamed: 5': 5,
'Unnamed: 6': 0},
{'Financial Ratios': 'Financial Expense Coverage',
'Unnamed: 1': 'Operating Profit/Total Financial Expense',
'Unnamed: 2': 0.6270806042,
'Unnamed: 3': 5.1835849804,
'Unnamed: 4': 1,
'Unnamed: 5': 25,
'Unnamed: 6': 5},
{'Financial Ratios': None,
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': 'Solvency Score',
'Unnamed: 5': '100/',
'Unnamed: 6': 25},
{'Financial Ratios': 'Profitability Ratios',
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': None,
'Unnamed: 5': None,
'Unnamed: 6': None},
{'Financial Ratios': 'Cumulative Profitability',
'Unnamed: 1': 'X2, Retained Earnings/Total Assets (RE/TA)',
'Unnamed: 2': 0.0236571651,
'Unnamed: 3': 0.1355888945,
'Unnamed: 4': 0,
'Unnamed: 5': 10,
'Unnamed: 6': 0},
{'Financial Ratios': 'Gross Profit Margin',
'Unnamed: 1': 'Gross Profit/Sales',
'Unnamed: 2': 0.1484981192,
'Unnamed: 3': 0.169457354,
'Unnamed: 4': 0,
'Unnamed: 5': 10,
'Unnamed: 6': 0},
{'Financial Ratios': 'Operating Profit Margin',
'Unnamed: 1': 'Operating Profit/Sales',
'Unnamed: 2': 0.0492337241,
'Unnamed: 3': 0.0634190537,
'Unnamed: 4': 0,
'Unnamed: 5': 15,
'Unnamed: 6': 0},
{'Financial Ratios': 'Net Profit Margin',
'Unnamed: 1': 'Net Profit/Sales',
'Unnamed: 2': 0.0334269397,
'Unnamed: 3': 0.1038080661,
'Unnamed: 4': 0,
'Unnamed: 5': 15,
'Unnamed: 6': 0},
{'Financial Ratios': 'Return on Assets-Productivity',
'Unnamed: 1': 'X3, Operating Profit/Total Assets',
'Unnamed: 2': 0.0181613627,
'Unnamed: 3': 0.0295913222,
'Unnamed: 4': 1,
'Unnamed: 5': 25,
'Unnamed: 6': 5},
{'Financial Ratios': 'Return on Equity',
'Unnamed: 1': 'Net Income/Equity',
'Unnamed: 2': 0.0608250368,
'Unnamed: 3': 0.1793879706,
'Unnamed: 4': 2,
'Unnamed: 5': 25,
'Unnamed: 6': 10},
{'Financial Ratios': None,
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': 'Profitability Score',
'Unnamed: 5': '100/',
'Unnamed: 6': 15},
{'Financial Ratios': 'Growth',
'Unnamed: 1': None,
'Unnamed: 2': 'Year over year trends 2012 vs 2013',
'Unnamed: 3': None,
'Unnamed: 4': None,
'Unnamed: 5': None,
'Unnamed: 6': None},
{'Financial Ratios': 'Sales Growth',
'Unnamed: 1': 'Annual Sales Growth',
'Unnamed: 2': 0.121184383,
'Unnamed: 3': None,
'Unnamed: 4': 1,
'Unnamed: 5': 20,
'Unnamed: 6': 20},
{'Financial Ratios': 'Operating Profit Growth',
'Unnamed: 1': 'Annual Operating Profit Growth',
'Unnamed: 2': 0.0492337241,
'Unnamed: 3': None,
'Unnamed: 4': 1,
'Unnamed: 5': 20,
'Unnamed: 6': 20},
{'Financial Ratios': 'NetProfit Growth',
'Unnamed: 1': 'Annual Net Profit Growth',
'Unnamed: 2': -0.6389706106,
'Unnamed: 3': None,
'Unnamed: 4': 0,
'Unnamed: 5': 20,
'Unnamed: 6': 0},
{'Financial Ratios': 'Tangible Net Worth Growth',
'Unnamed: 1': 'Annual Net Worth Growth',
'Unnamed: 2': 0.0647643295,
'Unnamed: 3': None,
'Unnamed: 4': 1,
'Unnamed: 5': 20,
'Unnamed: 6': 20},
{'Financial Ratios': 'Cash and cash equivalents Growth',
'Unnamed: 1': 'Annual Cash and cash equivalents Growth',
'Unnamed: 2': -0.503449854,
'Unnamed: 3': None,
'Unnamed: 4': 0,
'Unnamed: 5': 20,
'Unnamed: 6': 0},
{'Financial Ratios': None,
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': None,
'Unnamed: 5': None,
'Unnamed: 6': None},
{'Financial Ratios': None,
'Unnamed: 1': None,
'Unnamed: 2': None,
'Unnamed: 3': None,
'Unnamed: 4': 'Growth Score',
'Unnamed: 5': '100/',
'Unnamed: 6': 60},
{'Financial Ratios': 'General Financial Score',
'Unnamed: 1': 3,
'Unnamed: 2': 16.8,
'Unnamed: 3': 5.0,
'Unnamed: 4': 'General Financial Score',
'Unnamed: 5': '100/',
'Unnamed: 6': 36.8},
{'Financial Ratios': None,
'Unnamed: 1': 3,
'Unnamed: 2': 9,
'Unnamed: 3': None,
'Unnamed: 4': None,
'Unnamed: 5': None,
'Unnamed: 6': None}]
You can use this snippet of code :-
data = json.loads(<stringify_json>)
index_key_mapping = {index: key for index, key in enumerate(data.keys())}
formated_data = [{
index_key_mapping[index]: value for index, value in enumerate(ord_pair)
} for ord_pair in zip(*[
dictionary.values() for key, dictionary in data.items()
])]
You can use list and .items():
arr_fr = list(json_fr.items())
print('Child title: ', arr_fr[0][0])
print('Child value: ', arr_fr[0][1])
print('Child of child: ', arr_fr[0][1]['3'])
And I get result:

decode JSON strings into dicts and put them in a list, last, convert the list to JSON
json_list = []
json_list.append(json.loads(JSON_STRING))
json.dumps(json_list)
or more pythonic syntax
output_list = json.dumps([json.loads(JSON_STRING) for JSON_STRING in JSON_STRING_LIST])
Use json.dumps before json.loads to convert your data to dictionary object This also helps prevent valueError: Expecting property name enclosed in double quotes.
Ex:
import json
myJSONStringList = ['{"user": "testuser", "data": {"version": 1, "timestamp": "2018-04-03T09:23:43.388Z"}, "group": "33"}',
'{"user": "otheruser", "data": {"version": 2, "timestamp": "2018-04-03T09:23:43.360Z", }, "group": "44"}']
print([json.loads(json.dumps(i)) for i in myJSONStringList])
Output:
[u'{"user": "testuser", "data": {"version": 1, "timestamp": "2018-04-03T09:23:43.388Z"}, "group": "33"}', u'{"user": "otheruser", "data": {"version": 2, "timestamp": "2018-04-03T09:23:43.360Z", }, "group": "44"}']
In your for loop statement, Each item in json_array is a dictionary and the dictionary does not have a key store_details. So I modified the program a little bit
import json
input_file = open ('stores-small.json')
json_array = json.load(input_file)
store_list = []
for item in json_array:
store_details = {"name":None, "city":None}
store_details['name'] = item['name']
store_details['city'] = item['city']
store_list.append(store_details)
print(store_list)
If you arrived at this question simply looking for a way to read a json file into memory, then use the built-in json module.
with open(file_path, 'r') as f:
data = json.load(f)
If you have a json string in memory that needs to be parsed, use json.loads() instead:
data = json.loads(my_json_string)
Either way, now data is converted into a Python data structure (list/dictionary) that may be (deeply) nested and you'll need Python methods to manipulate it.
If you arrived here looking for ways to get values under several keys as in the OP, then the question is about looping over a Python data structure. For a not-so-deeply-nested data structure, the most readable (and possibly the fastest) way is a list / dict comprehension. For example, for the requirement in the OP, a list comprehension does the job.
store_list = [{'name': item['name'], 'city': item['city']} for item in json_array]
# [{'name': 'Mall of America', 'city': 'Bloomington'}, {'name': 'Tempe Marketplace', 'city': 'Tempe'}]
Other types of common data manipulation:
For a nested list where each sub-list is a list of items in the
json_array.store_list = [[item['name'], item['city']] for item in json_array] # [['Mall of America', 'Bloomington'], ['Tempe Marketplace', 'Tempe']]For a dictionary of lists where each key-value pair is a category-values in the
json_array.store_data = {'name': [], 'city': []} for item in json_array: store_data['name'].append(item['name']) store_data['city'].append(item['city']) # {'name': ['Mall of America', 'Tempe Marketplace'], 'city': ['Bloomington', 'Tempe']}For a "transposed" nested list where each sub-list is a "category" in
json_array.store_list = list(store_data.values()) # [['Mall of America', 'Tempe Marketplace'], ['Bloomington', 'Tempe']]