How to convert JSON string to array

0 votes

I want to take JSON as input from text area in php and use this input and convert it to JSON and pass it to php curl to send request.

I'm getting at php from get of api this json string i want to pass to json but it is not converting to array

echo $str='{
        action : "create",
        record: {
            type: "n$product",
            fields: {
                n$name: "Bread",
                n$price: 2.11
            },
            namespaces: { "my.demo": "n" }
        }
    }';
    $json = json_decode($str, true);

the above code is not returning me array.How to do that?

Aug 27, 2020 in PHP by kartik
• 37,520 points
141 views

1 answer to this question.

0 votes

Hello @kartik,

If you pass the JSON in your post to json_decode, it will fail. Valid JSON strings have quoted keys:

json_decode('{foo:"bar"}');         // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}');       // returns an object, not an array.

Hope it helps!!
Thank you!!

answered Aug 27, 2020 by Niroj
• 82,760 points

Related Questions In PHP

0 votes
1 answer

How to check if a string is JSON or not?

Hello @kartik, Use JSON.parse function isJson(str) { ...READ MORE

answered Aug 19, 2020 in PHP by Niroj
• 82,760 points
1,635 views
0 votes
1 answer

How to convert a string to date in mysql?

Hello @kartik, you can do SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y') FROM ...READ MORE

answered Aug 20, 2020 in PHP by Niroj
• 82,760 points
621 views
0 votes
1 answer

How to convert multidimensional array into single array?

Hello, This single line would do that: $array = ...READ MORE

answered Nov 6, 2020 in PHP by Niroj
• 82,760 points
1,294 views
0 votes
1 answer

How to use a string path to set nested array data?

Hello @kartik, Use the reference operator to get ...READ MORE

answered Nov 23, 2020 in PHP by Niroj
• 82,760 points
256 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,760 points
9,583 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,760 points
622 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,760 points
2,034 views
+1 vote
2 answers

How to set cache false for getJSON in jQuery?

You can't pass any configuration parameters to ...READ MORE

answered Oct 7, 2020 in JQuery by Amit
• 140 points
632 views
0 votes
1 answer

How to convert a string to JSON object in PHP?

Hello @kartik, it seems that your JSON is ...READ MORE

answered Oct 20, 2020 in PHP by Niroj
• 82,760 points
756 views
0 votes
1 answer

How to pass an array within a query string?

Hello, Submitting multi-value form fields, i.e. submitting arrays ...READ MORE

answered Apr 7, 2020 in PHP by Niroj
• 82,760 points
6,759 views