0

I have a string that appears like an object but actually is a string. It does not look very nice and comfortable to read. Are there any possibilities to change this? JSON.parse does not seem to work because not every element meets the requirements to be a JSON object. For example, number 0 does not have any quotes. Any ideas?

{'credits': [{'api_user_id': '1', 'amount': '897.26', 'l_amount': '0.00', 'period': '90', 'charge': '338.77', 'o_charge': '338.77', 'created': '2019-11-20 12:46:56', 'registered': '2019-11-20 12:46:56', 'expire': '2020-02-20', 'max_delay': '0', 'delay': 0, 'debt': '0.00', 'late_fee': '0.00', 's_payment': '0.00', 'discount_code': '', 'description': 'reg', 'p_amount': '0.00', 'paid': '0000-00-00 00:00:00',  'origin_id': 0, 'confirmed': '0', 'l_interest': '0.00', 'l_administrative_fee': '0.00', 'l_cinterest': '0.00', 'l_late_fee': '0.00', 'l_penalty': '0.00', 'l_s_amount': '0.00', 'l_s_interest': '0.00', 'l_s_administrative_fee': '0.00', 'important': 0, 'schedule': [{'date': '2019-12-20', 'day': 30, o: '897.26', 'c': '0.00', 'p': '160.61', 's': '251.40', 'amount': '412.01'}, ]}
Barrosy
  • 1,407
  • 2
  • 25
  • 56
anna
  • 859
  • 2
  • 9
  • 23
  • I think you are looking for a way to [deserialize](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to) JSON. You might want to create a C# solution. You might want to look into the following method: `System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(json);`. – Barrosy Nov 22 '19 at 09:36
  • [This question](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) might also help you as a reference. Please add to your question what you already tried. For any reference when asking questions, please visit [How-to-ask](https://stackoverflow.com/help/how-to-ask). Make sure to add anything you have already tried. – Barrosy Nov 22 '19 at 09:43
  • Thank you. The problem is this that text is created dynamically, and I can not change what I get. Problem is when I have many txts which are different. – anna Nov 22 '19 at 09:54

2 Answers2

2

OK, so problem with your string is You have used single quotes. So instead of this:

var str = {'credits': [{'api_user_id': '1', 'amount': '897.26','l_amount': '0.00', 'period': '90', 'charge': '338.77', 'o_charge': '338.77', 'created': '2019-11-20 12:46:56', 'registered': 2019-11-20 12:46:56', 'expire': '2020-02-20','max_delay': '0', 'delay': 0, 'debt': '0.00', 'late_fee': '0.00', 's_payment': '0.00', 'discount_code': '', 'description': 'reg', 'p_amount': '0.00', 'paid': '0000-00-00 00:00:00',  'origin_id': 0, 'confirmed': '0', 'l_interest': '0.00', 'l_administrative_fee': '0.00', 'l_cinterest': '0.00', 'l_late_fee':'0.00', 'l_penalty': '0.00', 'l_s_amount': '0.00', 'l_s_interest': '0.00', 'l_s_administrative_fee': '0.00', 'important': 0, 'schedule': [{'date': '2019-12-20', 'day': 30, o: '897.26', 'c': '0.00', 'p': '160.61', 's': '251.40','amount': '412.01'}, ]}

Use your string like this and JSON.parse will work

const str = '{"credits":[{"api_user_id":"1","amount":"897.26","l_amount":"0.00","period":"90","charge":"338.77","o_charge":"338.77","created":"2019-11-20 12:46:56","registered":"2019-11-20 12:46:56","expire":"2020-02-20","max_delay":"0","delay":0,"debt":"0.00","late_fee":"0.00","s_payment":"0.00","discount_code":"","description":"reg","p_amount":"0.00","paid":"0000-00-00 00:00:00","origin_id":0,"confirmed":"0","l_interest":"0.00","l_administrative_fee":"0.00","l_cinterest":"0.00","l_late_fee":"0.00","l_penalty":"0.00","l_s_amount":"0.00","l_s_interest":"0.00","l_s_administrative_fee":"0.00","important":0,"schedule":[{"date":"2019-12-20","day":30,"o":"897.26","c":"0.00","p":"160.61","s":"251.40","amount":"412.01"}]}]}'
const strData = JSON.parse(str);


console.log(strData)
Parth Savaliya
  • 353
  • 6
  • 13
0

Try this

var obj1 = eval("(" + json + ')');

or this help https://jsonformatter.curiousconcept.com/