Andre Retro Pie
Andre Retro Pie

Reputation: 53

Pseudo-JSON breakdown

I'm having code like this (can't change its nature) and what I want to do is to break it down to access data in a JSON-esque way

product:{
    name:[
      {
        language:"en_US",
        value:"Apocalypse Now"
      }
    ],
    description:[
      {
        language_tag:"en_US",
        value:"Good!"
      }
    ]
}

So I would like to access it like product.name.value.

I tried different Regexp solutions as it's not a real JSON (missing quotation marks amongst other things) but I cannot seem to get it sturdy enough to isolate information in a proper way.

Any help is welcome

Upvotes: 0

Views: 366

Answers (1)

georg
georg

Reputation: 214959

Regular expressions are not the way to go. You can't parse a programming language with regexes. Your realistic options are

  • eval (fine if input is trusted and performance is not a concern)
  • a full-blown Javascript parser: esprima
  • a javascript-to-json parser (mine): litr

Upvotes: 1

Related Questions