0
x=json.dumps(result)
print(x)

Output:

['Client Name:'Sriram',
 'Trade Date :'03-09-2019',
 'Security :CERAMICIST-ICICLE,NAT CO - PHARMA',
 'Quantity:14,2',
 'Net Rate: 145.7500,552.3725',
 'Buy/Sell :'Buy ', 'Buy',
 'Net Total:2040.5000,1104.7450']

and I want to convert this list into a json format like this:

{'Client Name': Sriram',
 'Trade Date' :03-09-2019',
 'Security' :['CERAMICIST-ICICLE','NAT CO - PHARMA',
 'Quantity':'14,2',
 'Net Rate':['145.7500','552.3725'],
 'Buy/Sell':['Buy','Sell',
 'Net Total':['2040.5000','1104.7450']
sonu
  • 15
  • 4
  • please share your list – Adam Strauss Feb 19 '20 at 05:22
  • ['TradeDate :03-09-2019', 'Security :ICICIPRAMCICICI500', 'Quantity :14', 'NetSale: 145.7500', 'Buy/Sell :2040.5000BuyINF109KC1CZ3'] – sonu Feb 19 '20 at 05:31
  • hold on... your result is not valid json. do you mean `{'TradeDate' : '03-09-2019', ...}` or `['TradeDate :03-09-2019',....]` ? – Adrian Shum Feb 19 '20 at 05:51
  • If someone has solved your problem, don't forget to upvote and accept the answer. If your problem is still problem, do comment what's the issue! – abhiarora Feb 24 '20 at 04:29
  • yes issue is there – sonu Feb 26 '20 at 11:13
  • {'Client Name': Sriram', 'Trade Date' :03-09-2019', 'Security' :['CERAMICIST-ICICLE','NAT CO - PHARMA', 'Quantity':'14,2', 'Net Rate':['145.7500','552.3725'], 'Buy/Sell':['Buy','Sell', 'Net Total':['2040.5000','1104.7450'] – sonu Feb 26 '20 at 11:16
  • output should come like this – sonu Feb 26 '20 at 11:16

2 Answers2

0

One Liner in Python using dict comprehension. We need to first convert it in dict using dict comprehension and then use json.dumps() to convert it to json format.

li = ['TradeDate :03-09-2019',
 'Security :ICICIPRAMCICICI500',
 'Quantity :14',
 'NetSale: 145.7500',
 'Buy/Sell :2040.5000BuyINF109KC1CZ3']


di = {i.split(':')[0]:i.split(':')[1] for i in li}
print(di)
js = json.dumps(di)
print(js)

Outputs:

{'TradeDate ': '03-09-2019', 'Security ': 'ICICIPRAMCICICI500', 'Quantity ': '14', 'NetSale': ' 145.7500', 'Buy/Sell ': '2040.5000BuyINF109KC1CZ3'}

{"TradeDate ": "03-09-2019", "Security ": "ICICIPRAMCICICI500", "Quantity ": "14", "NetSale": " 145.7500", "Buy/Sell ": "2040.5000BuyINF109KC1CZ3"}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

As I have mentioned in comment, your result is not a valid JSON.

Let me have these assumption:

  1. You have a list of string, and for each string it is in form of key : value
  2. You want a JSON string like
    { "key1" : "value1",
      "key2" : "value2",...}
    
    (i.e. all key and values are string)

You could just construct a dict, with each line in the list become an entry of dict, and then use json.dumps.

If you need to preserve the type of the value (e.g. 123 should be a number instead of string: "key1" : 123), then it is more complicated: you may need to check the format of the value and convert it to correct type

Another choice is, as key: value is a valid entry in YAML (note the missing double quote and the space after colon), you can do simple massaging in your message and construct a yaml string, use yaml parser to parse it to a dict, and then use json parser to construct a json string. Something like:

import yaml
import json
l = [ "a:b", "c: 123", "d: true"]
result = json.dumps(
            yaml.load("\n".join( 
                      [ t[0] + ": " + t[1] for t in 
                          [ s.split(":", 1) for s in l]
                      ])))
# result string is '{"a": "b", "c": 123, "d": true}'
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131