Need help extracting JSON data

I've got the following JSON structure and I need to access the content inside the square brackets []. Is there a way to access using the "spirits" node, or do I need to use preg_match to get the content between the brackets?

jsonstamp({
	"product":{
		"item":{
			"videofeed":29,
			"page":120,
			"weight":"12oz",
			"audiofeed":"14dbl",
			"taxable":1
		},"item":{
			"videofeed":591,
			"page":124,
			"weight":"18oz",
			"audiofeed":"14dbl"
			"taxable":1
		},"item":{
			"videofeed":705,
			"page":144,
			"weight":"20oz",
			"audiofeed":"14dbl"
			"taxable":1
		},"item":{
			"spirits":
			[{
				"id":1864,
				"price":"$12.50",
				"type":"whisky,
				"taxable":0
			},{
				"id":1865,
				"price":"$12.50",
				"type":"porter",
				"taxable":0
			},{
				"id":1866,
				"price":"$13.50",
				"type":"whisky3,
				"taxable":1
			}],
			"videofeed":1866,
			"page":184,
			"weight":"12oz",
			"audiofeed":"19dbl",
			"taxable":0
		},"item":{
			"videofeed":1592,
			"page":1192,
			"weight":"14oz",
			"audiofeed":"14dbl",
			"taxable":1
		}
	})

Open in new window


I'd like to return the content in a JSON format and with no whitespace like this:
[{"id":1864,"price":"$12.50","type":"whisky,"taxable":0},{"id":1865,"price":"$12.50","type":"porter","taxable":0},{"id":1866,"price":"$13.50","type":"whisky3,"taxable":1}]

Open in new window


LVL 1
befidledAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BardobraveCommented:
Well... if you eval a JSON string on javascript you'll obtain an object with the structure defined by the JSON string, this is why JSON is so powerful on javascript environment.

Looking at your JSON string it's strange to me that you have so many "item" elements, I think you should use some sort of collection to store each "item" class element, something like:

jsonstamp({
	"product":{
		"items":[ {
			"videofeed":29,
			"page":120,
			"weight":"12oz",
			"audiofeed":"14dbl",
			"taxable":1
		},{
			"videofeed":591,
			"page":124,
			"weight":"18oz",
			"audiofeed":"14dbl"
			"taxable":1
		},{
			"videofeed":705,
			"page":144,
			"weight":"20oz",
			"audiofeed":"14dbl"
			"taxable":1
		},{
			"spirits":
			[{
				"id":1864,
				"price":"$12.50",
				"type":"whisky,
				"taxable":0
			},{
				"id":1865,
				"price":"$12.50",
				"type":"porter",
				"taxable":0
			},{
				"id":1866,
				"price":"$13.50",
				"type":"whisky3,
				"taxable":1
			}],
			"videofeed":1866,
			"page":184,
			"weight":"12oz",
			"audiofeed":"19dbl",
			"taxable":0
		},{
			"videofeed":1592,
			"page":1192,
			"weight":"14oz",
			"audiofeed":"14dbl",
			"taxable":1
		} ]
	})

Open in new window


This way, once you load your whole class into an object, you could retrieve your spirits just looping through item array in search of spirits value.

var x = 0;
var found = false;
var spirits;
while (x < yourObject.items.length and !found) {
   if (yourObject.items[x].spirits != null) {
       spirits = yourObject.items[x].spirits;
       found = true;
   }
   x++;
}

Then you'll have an spirits array loaded with your data, and you can return it as a JSON string simply looping through it and building a string.
0
Ray PaseurCommented:
Where have you "got" this structure?  Is it in your server-side PHP script or in your browser-side JavaScript?
0
Slick812Commented:
greeting befidled, , I follow what you have as the JSON for input, and what you want out of it, also json, There is the php json_decode( ), but you will still have to parse that output to extract and convert it to your single line string, , You may as well parse the original, I do a space and line break removal then search for '{"spirits":[{' and then go to the next ']' and extract out exactly what you need.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.