0

I know that there is a method json.loads(string) but it will work only if I got String formatted to the JSON style. The String I have is in this form:

{
data1: {
    x1: 'xyz'
},
data2 {
    y1: 'datadata'
 },
identify: {
    title: {
       text: 'Some important things'
     }
    }
}

Is there any trick to do that?

shurrok
  • 795
  • 2
  • 13
  • 43
  • 1
    What you've got is not JSON. It's not even nearly JSON. So the json library is not going to help you. If it's some other kind of standard format, you might be able to find a tool, otherwise I think you're going to have to write something custom for yourself. – khelwood Oct 27 '17 at 13:33
  • @khelwood I know it is not JSON becouse it has no quotes, you don't need to tell me that. That's why I asked the question! – shurrok Oct 27 '17 at 13:34
  • 2
    I'm confused. Do you want to convert TO or FROM json? Your title says "to" but you said you tried `loads`, which converts _from_ json. – Kevin Oct 27 '17 at 13:36
  • Your input data has a function declaration inside. How would you encode a function to JSON? – Aleksandr Borisov Oct 27 '17 at 13:36
  • If the quotes was the only hurdle, it would be easy to parse it... but, as @AleksandrBorisov says, it's much more about having calls to functions! – Jblasco Oct 27 '17 at 13:38
  • looks like a JSON kind of thing for javascript? – Jblasco Oct 27 '17 at 13:39
  • the `function` means that it is just full-fledged Javascript, not even an alternate coding for a json data structure. Otherwise, [`demjson`](https://github.com/dmeranda/demjson) could parse it in non-strict mode. – Antti Haapala -- Слава Україні Oct 27 '17 at 13:45
  • Well, i want Transform current input into JSON. Should I use `dump()` method then? But I suppose there will be a problem with no quotes too. Am I right? – shurrok Oct 27 '17 at 13:56
  • I know its function there, I have my own way to deal with that, the question is not about this – shurrok Oct 27 '17 at 13:58
  • Ok, I removed the function from question to make everything clear. I have problem only with no quotes – shurrok Oct 27 '17 at 14:00
  • 1
    Add the quotes by parsing the whole text, then... You can use regular expressions or you can just find the ':' in your text and add the quotes to them! – Jblasco Oct 30 '17 at 11:40
  • @Jblasco is there a one line solution for this? – shurrok Oct 30 '17 at 12:53
  • Anything can be written in one line, but it is not always convenient or clear to do so. This is one of those cases in which two or three lines are needed for clarity – Jblasco Oct 30 '17 at 14:49

1 Answers1

2

I cannot stress enough how clunky I think this solution is, but it does the job. First, I'm assuming the OP made a typo and meant "data2**:**" or this solution will need to be even more complex.

First, create a function that includes the much needed quotation marks.

def fix_element(elem):
if elem[-1] == ':':
    return '"{}":'.format(elem[:-1])
else:
    return elem

Second, parse the text of your object, using only double quotes:

    text = """{
data1: {
    x1: 'xyz'
},
data2: {
    y1: 'datadata'
 },
identify: {
    title: {
       text: 'Some important things'
     }
    }
}""".replace("\'", '"')enter code here

Then correct all elements of the text:

fixed_elem = [fix_element(elem) for elem in text.split()]
fixed_text = ''.join(fixed_elem)

Possibly a solution based on regular expressions would work more succinctly, but I don't have the time or desire to find the correct expressions, to be honest.

Jblasco
  • 3,827
  • 22
  • 25