How to arrange JSON without quotation marks in index

Asked

Viewed 637 times

3

I have the following JSON that came from a Javascript and I’m using it in PHP. I’m giving json_decode($json), but obviously it doesn’t work. Because there are no quotes in the index of every value.

   options: [
          {
            value: "120",
            id: "40",
            title: "1400g",
            name: "Tamanho"
          },
          {
            value: "336",
            id: "60",
            title: "Chocolate",
            name: "Sabor"
          }
        ]

How can I fix this in PHP?

  • How does this JSON get to PHP? Ideally it would be for the sender, not the sender. If you’re sending it by Javascript, a JSON.stringify should generate a valid JSON.

  • I am reading the DOM of a product page and the JSON is inside a javascript that is used on the page, I have no way to choose how I will receive unfortunately

  • Did it work Vitor? If it worked the answer. Put it as right. I advise you to choose the answer of Lipespry. ✔

  • 1

    Take a look at syntax of JSON to see that what you have is at most an incomplete piece of a JSON. Maybe you should take a step back and rethink whether this is the best solution for whatever you’re trying to do, because taking an incomplete chunk of Javascript and trying to interpret in PHP doesn’t seem like a good approach.

  • It is really a piece of json, after all json has more than 1000 positions so there is not much to post here, I will test the Lipespry and already produce the upvote if it works

2 answers

4


"- I have the following JSON [...]"

No. That’s not a JSON!

jsonlint.com

You can validate this and other JSON on jsonlint.com.


"- [...] no quotes on the index of each value [...] How can I fix this in PHP?"


"- If you are sending by Javascript, a JSON.stringify should generate a valid JSON." - @user140828


"- I can’t choose how I’ll receive unfortunately"

So the way is to appeal to a "gambiarra"...

Whereas your "JSON" follows at all times this pattern, you can set a simple regular expression to "add quotes":

$json = '{'.preg_replace('/(\w+)(\:)/', '\"$1\"$2', $json).'}';

See the expression working on regex101.com.

  • your regex is better than mine!

  • I need to practice that there... everyone does regex better than me... XD kk

  • 2

    Wait then until the @hkotsubo show up and give a blessed lecture on Regex! kkkk

  • 2

    Yes! That’s it... We lost... =)

  • 1

    But your answer is the right one!

  • 1

    @Andrei would not consider the part of the gambiarra a correct answer. It is more a gambiarra even! Kkkk

  • 2

    Haha! Well, I have nothing to add, maybe just a detail: if the value has : between quotes, then replace produces an invalid JSON, see (and a possible solution). But as it is not clear whether this can happen, then your regex does. And I don’t think it’s gambiarra, the problem is the fact that you’re taking a chunk of Javascript (and it’s not even a complete chunk, because in JS it’s not a valid object either), so the approach already seems to have started wrong, so no solution will be good :-)

  • 2

    @Andreicoelho I suggest this site and that too, has very good examples. And that book is considered one of the "bibles" of regex (and this goes deep even, I still can’t understand everything...)

  • It is! Precisely why I said that "if always follow this pattern" are useful if. And yes, everything started wrong! Kkkk

  • @hkotsubo worth master! I will see yes!

Show 5 more comments

2

You can replace it with a regex. So:

$jsonEstranho = 'options: [
      {
        value: "120",
        id: "40",
        title: "1400g",
        name: "Tamanho"
      },
      {
        value: "336",
        id: "60",
        title: "Chocolate",
        name: "Sabor"
      }
]';

// retira todas as aspas duplas
$jsonEstranho = str_replace('"', "", $jsonEstranho); 
// insere aspas duplas em tudo que não vor um caracter do Json e colo chaves no inicio e no fim
$jsonCorreto = "{".preg_replace('/(\w+)/', '"$0"', $jsonEstranho)."}";  

print_r(json_decode($jsonCorreto)); 

See working

  • +1 for $jsonEstranho! kkkk

  • @Lipespry kkkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.