JSON help

Waffl3s

Well-Known Member
Joined
May 9, 2011
Messages
103
Reaction score
14
hi,

jnr dev here and need help. I'm trying to get the value of the item name but because I am using JSON.stringify.split(":") I get Palony and not the full name. Is there a way to do this without changing the JSON.stringify.split portion?
Code:
{
  "id": "12345",
  "item_name": "Palony:Pork"
}

Please help
 
One way to do this, is to use jQuery and specifically query the key value pair you are looking for..

Don't ask me how, I am not a dev, just fiddle way more with JSON objects than I care to mention..

At a guess though, something like creating a variable as an array and then jQuery.parse your JSON object and then from there query to grab specific key value pairs you are looking for..
 
Language?
In python you'd do something like:
import json
weather = urllib2.urlopen('url')
wjson = weather.read()
wjdata = json.loads(wjson)
print wjdata['data']['current_condition'][0]['temp_C']
https://stackoverflow.com/questions/16129652/accessing-json-elements
In JS it's already an array if you set it to a variable, just access using arrayName.index (dot notation) or arrayName["index"] (bracket notation).
https://www.w3schools.com/js/js_json_objects.asp

There's no need to stringify it as you already have it ordered by key/index -> value. Most of the time if you stringify, it's when you've sent it as one long string (e.g. save/load file) and then want to tokenize it so you can "make use" of it. The reason we use JSON is that it is structured on send/receive.
 
Simple things....

Code:
var testObject = {
  "id": "12345",
  "item_name": "Palony:Pork"
}

console.log(testObject.item_name) //Unsplit - as requested in your original answer

//Split the name into its two parts
var itemNames = testObject.item_name.split(':')
console.log(itemNames[0])
console.log(itemNames[1])

As above , since you already have valid JSON , Javascript will automatically turn it into an object for you. You dont need to turn it into a string. Then , you just access the name from the object and split it via a ":"

Heres the working code

https://jsfiddle.net/#&togetherjs=O4QoeE959F

Youre not giving us the full info though. Why do you not want to change JSON.stringyfy.split() ?
 
Last edited:
Firstly, your code is fragile.
polonium::swine, is going to break if you split it. And if the code generating it catches a wobble, and puts in doubles, or changes the convention, your data will fail.

So, how do you handle this?

Do you have access to the dev/code that generates this? If you do, sit down and document the intended convention.

Secondly, consider using a type key
So your json looks like this...

item_name: bolony,
type: bork

Why? This way you can filter, sort and isolate various permutations.

Think about how the data will be used, now and 3 months from now. Systems evolve and change as the business requirements change, or grow. If time allows, sit down and map out the parts of the app, and the ebb and flow of data in, data out. Your future self will thank you.

Also don't be afraid of adding more key value pairs, but only if it makes sense, like in this context. Searching keys is much faster than splitting, and provides you with a better mental model of the structure of the data.

Shout if you need more help.

As an aside, even js juniors need to know how to wrangle objects... If you can't do it at 2am without google or stackoverflow, it's time to hit the books.
 
Last edited:
If that's a JSON string;, parse it first var obj = JSON.Parse(input); and then you can access it the way @retromodcoza described.
 
hi,

jnr dev here and need help. I'm trying to get the value of the item name but because I am using JSON.stringify.split(":") I get Palony and not the full name. Is there a way to do this without changing the JSON.stringify.split portion?
Code:
{
  "id": "12345",
  "item_name": "Palony:Pork"
}

Please help

Can't you just add more quotes around it?

Code:
{
  "id": "12345",
  "item_name": "'Palony:Pork'"
}
 
JS

let item;

let someObject = {
"id": "12345",
"item_name": "Palony: Pork"
}

item = someObject.item_name;
console.log(item);
 
Top
Sign up to the MyBroadband newsletter
X