1

I have the following string that I receive from an API call:

a = "{
      "option1"=>"Color",
      "attribute1"=>{0=>"Black", 1=>"White",2=>"Blue"},
      "option2"=>"Size",
      "attribute2"=>{0=>"S", 1=>"L",2=>"M"}
}"

I would like to convert it to a JSON array; So, I have tried JSON_encode(), but it returns the following string:

""{\"option1\"=>\"Color\",\"attribute1\"=>{0=>\"Black\", 1=>\"White\",2=>\"Blue\"},\"option2\"=>\"Size\",\"attribute2\"=>{0=>\"S\", 1=>\"L\",2=>\"M\"}}"" 

Could you please advise me on how to achieve what i want.

Thanks

6
  • 1
    You should do JSON_decode() since your string i'ts JSON. Use the option of json decode to parse as an array instead of \StdClass. (OFC you need the right line separators and symbol escapes). Commented Jan 9, 2017 at 8:21
  • 1
    Where does that string come from? Without knowing what format that should be there is little strategy to parse and evaluate it... Commented Jan 9, 2017 at 8:28
  • 1
    @YuriBlanc That string is not valid JSON. Commented Jan 9, 2017 at 8:31
  • 1
    @JYoThI That string is not valid JSON. Commented Jan 9, 2017 at 8:41
  • 1
    yeah just now i see the mistake that's not valid json @arkascha Commented Jan 9, 2017 at 8:44

1 Answer 1

3

The preferable way would be affecting the service which gives you such kind of strings to get a valid JSON string(if it's possible).
At the moment, if it's about adapting some "arbitrary" string to JSON notation format and further getting a JSON "array" use the following approach with preg_replace and json_decode functions:

$json_str = '{
      "option1"=>"Color",
      "attribute1"=>{0=>"Black", 1=>"White",2=>"Blue"},
      "option2"=>"Size",
      "attribute2"=>{0=>"S", 1=>"L",2=>"M"}
}';

// To get a 'pure' array
$arr = json_decode(preg_replace(["/\"?(\w+)\"?=>/", "/[\r\n]|\s{2,}/"], ['"$1":', ''], $json_str), true);
print_r($arr);

The output:

Array
(
    [option1] => Color
    [attribute1] => Array
        (
            [0] => Black
            [1] => White
            [2] => Blue
        )

    [option2] => Size
    [attribute2] => Array
        (
            [0] => S
            [1] => L
            [2] => M
        )
)

To get a JSON string representing an array:

$json_arr = json_encode($arr);
print_r($json_arr);

The output:

{"option1":"Color","attribute1":["Black","White","Blue"],"option2":"Size","attribute2":["S","L","M"]}
Sign up to request clarification or add additional context in comments.

5 Comments

Exactly what I want to achieve. thank you for your assistance
This doesn't seem to handle the a = "..." part but, it it's constant, a simple mb_substr() call should do.
@ÁlvaroGonzález, you mean, when the input string contains only "..." without any structure prototype?
I mean when the input string contains literally what the question says (though I suspect it isn't real data at all, since the question actually contradicts itself.)
@ÁlvaroGonzález, actually it looks like a "dumped" object/array not a valid API response. That's why I wrote about affecting the service ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.