-2

im new with JSON and have had massive struggle trying to parse this JSON

    {
    "730": {
        "success": true,
        "data": {
            "price_overview": {
                "currency": "USD",
                "initial": 1499,
                "final": 1499,
                "discount_percent": 0
            }
        }
    }
}

I have tried different approaches but still struggle to get the value of initial in price_overview

Brad m
  • 97
  • 1
  • 1
  • 10
  • 1
    Possible duplicate of [Parsing JSON file with PHP](https://stackoverflow.com/a/4343691/6521116) – LF00 Jun 10 '17 at 23:39

2 Answers2

1

You need to json_decode, and then, just use the dictionary that is generated to grab the value. Like this:

$json = <<< JSON
    {
    "730": {
        "success": true,
        "data": {
            "price_overview": {
                "currency": "USD",
                "initial": 1499,
                "final": 1499,
                "discount_percent": 0
            }
        }
    }
}
JSON;

$json_a = json_decode($json, true);
echo $json_a['730']['data']['price_overview']['initial'];

CodePad

http://codepad.org/i1ELBxd9

Further Reading

Using JSON with PHP

Noah Cristino
  • 757
  • 8
  • 29
1

Try this:

<?php
$data = '{"730":{"success":true,"data":{"price_overview":{"currency":"USD","initial":1499,"final":1499,"discount_percent":0}}}}';

$json = json_decode(trim($data), true);

echo '<pre>';
print_r($json[730][data][price_overview]);
echo '</pre>';
Ravi Gehlot
  • 1,099
  • 11
  • 17