HI I am trying to create a JSON file in which I want to store some data for different files. The problem is I cannot figure the correct syntax. Here is what I have so far:
var object = {
"id-1" :[
{
"type":"Corporate Website",
"tech":"HTML" ,"CSS" , "Javascript/jQuery"
}
],
"id-2" :[
]
}
I seem to be getting an error at "tech".If that is not corect how can I enumarate multiple elements?I am sorry for the noob question I have been using javascript for a short period of time and I am still very confused with the language.
{
"id-1": [
{
"type": "Corporate Website",
"tech": [
"HTML",
"CSS",
"Javascript/jQuery"
]
}
],
"id-2": []
}
Note the array like syntax for "tech".
Tech should be an array (enclosed in square brackets):
"tech": ["HTML", "CSS", "Javascript/jQuery"]
Source:
An array is an ordered collection of values. An array begins with [
(left bracket) and ends with ] (right bracket). Values are separated
by , (comma).
http://www.json.org/
Related
I'm retrieving hashmap data from an api into my react app. the data looks like this..
[
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,"pic3.jpg"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,"me.jpg"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,"img-004.jpeg"]"
]
]
I need to render this data into a table.
Using map and with with data.slice(0, 1) I've managed to pull out the key (the long string), but I need help separating the name, number and optional image. I tried experimenting with various operators. I thought this would work
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
but it just returns ["
I'm hoping there is a simpler way to do it!
You presented the data in a strange format, but this works:
const data = [
[
"d243dc7-6fe8-151b-4ca8-1be528f2b36",
"[\"Jonny\",70]"
],
[
"8affa17-76d1-13e-6380-7cd2a3e3647",
"[\"Lucy\",106,\"pic3.jpg\"]"
],
[
"841cb28-24c7-872-3c66-63253800c8d",
"[\"Mike\",0]"
],
[
"6128e-182-cfb4-708b-c40a3ba2e6e",
"[\"Elodie\",23,\"me.jpg\"]"
],
[
"e55ef4c-8d41-3be4-27d-aae53330584",
"[\"Jacob\",9,\"img-004.jpeg\"]"
]
];
const data1 = JSON.parse(data.slice(1, 2)[0][1]);
console.log('name', data1[0]);
console.log('numb', data1[1]);
console.log('file', data1[2]);
{data.slice(1, 2).toString().substring(2,data.slice(1, 2).length-2)}
^ ^^^^^^^^^^^^^^^^
In the string "["Jonny",70]" , you only want to skip the first character "[", so your first parameter of substring() should be index 1
you forgot to transfer data.slice(1, 2) to string, so it's still an array and its length would be 1
so your code should be revised to:
data.slice(1, 2).toString().substring(1, data.slice(1, 2).toString().length - 2)
Using node.js(javascript) how do I access the GetDataResult node in this JSON data that has been converted from SOAP data.
{
"s:Envelope": {
"$": {
"xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/"
},
"s:Body": [{
"GetDataResponse": [{
"$": {
"xmlns": "http://tempuri.org/"
},
"GetDataResult": ["You entered: TEST"]
}]
}]
}
}
Test using nodejs interactive mode :
$ node
> var x = {
... "s:Envelope": {
..... "$": {
....... "xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/"
....... },
..... "s:Body": [{
....... "GetDataResponse": [{
......... "$": {
........... "xmlns": "http://tempuri.org/"
........... },
......... "GetDataResult": ["You entered: TEST"]
......... }]
....... }]
..... }
... }
undefined
> console.log(x["s:Envelope"]["s:Body"][0]["GetDataResponse"][0]["GetDataResult"][0])
Output :
'You entered: TEST'
Explanations :
I try to elaborate a bit from comments below. There is no container, I try to explain :
You have to think json like what it is : an object or a data structure.
In python, we would say it's a dict, in perl a hash table etc... Globally, it's all about associative array
So when you see in JSON :
"key" : { "value" }
it's an associative array
If instead you see
"key": [
{ "key1": "foo" },
{ "key2": "bar" },
{ "key3": "base" }
]
It's an array of hashes or array of associative arrays.
When you access a simple associative array without spaces or odd characters, you can (in js do :
variable.key
In your case, you have odd character : in the key name, so x.s:Envelope wouldn't work. Instead we write: x['s:Envelope'].
And as far as you have arrays of associative arrays inside [], you have to tell js which array number you need to fetch. It's arrays with only one associative array, so it's simple, we go deeper in the data structure by passing array number, that's what we've done with
x['s:Envelope']["s:Body"][0]
^
|
I'm sending a JSON array to a script for further processing. The JSON array contains a bunch of objects each of which contain a further array of objects. What I need to know is how to access values within those nested objects. So, for instance, if the script receives the following:
petlist = [
{"cats":[
{"catName":"Felix","catType":"British short haired"}
]
},
{"dogs":[
{"dogName":"Fido","dogType":"Labrador"}
]
},
{"fish":[
{"fishName":"Bob","fishType":"Goldfish"}
]
},
{"birds":[
{"birdName":"Polly","birdType":"Parrot"}
]
}
]
How would I then address, say, a) dogName, b) birdType, or c) the entire cats object?
Also, am I correct in my terminology here? As I understand it the stuff in square brackets is an array, while the stuff in curly braces is an object.
edit: I am building the JSON in Javascript and I then need to access the elements in a Jade template (in an 'each' loop)
Thanks
I changed your JSON a little bit because I think it was not very fun to work with. Basically I just loop through the objects thats why I thought you should have a key like name instead of dogName, catName and so on.
You can find the working example with Jade in this JSFiddle
HTML
<div id="jadeoutput"></div>
<pre id="jadeinput" style="display:none">
- console.log(petlist)
h1 List
ul.list
- for(var i in petlist)
li= "Item - "+ petlist[i].name
- for(var j in petlist[i].pets)
li= "Pet - " + petlist[i].pets[j].name + " " + petlist[i].pets[j].type
</pre>
JavaScript
$(function() {
var json = {
"petlist" : [
{
"name" : "cats",
"pets":
[
{ "name":"Felix","type":"British short haired"}
]
},
{
"name" : "dogs",
"pets":
[
{"name":"Fido","type":"Labrador"}
]
},
{
"name" : "fish",
"pets":
[
{"name":"Bob","type":"Goldfish"}
]
},
{
"name" : "birds",
"pets" :
[
{"name":"Polly","type":"Parrot"}
]
}
]};
$("#jadeoutput").html(jade.compile($("#jadeinput").html())(json));
});
I have a collection that looks like the ff:
{
"word":"approve",
"related" : [
{
"relationshipType" : "cross-reference",
"words" : [
"note"
]
},
{
"relationshipType" : "synonym",
"words" : [
"demonstrate",
"ratify",
]
}
],
},
{
"word": "note",
"related" : [
{
"relationshipType" : "synonym",
"words" : [
"butt",
"need",
],
},
{
"relationshipType" : "hypernym",
"words" : [
"air",
"tone",
]
},
{
"relationshipType" : "cross-reference",
"words" : [
"sign",
"letter",
"distinction",
"notice",
]
},
],
}
I want to group/categorize all the objects which have a word in another object, be it
the name (as the cross-reference field of 'approve', has note. searches for the word 'note'.
or
the word is in another object's related words. like having a synonym of 'ratify' under 'approve' then looking for other objects that have have 'ratify' in any field of their related words.
Then save these to a new collection called categories.
result should Be:
{
"category": 1,
"words":["approve","note"],
}
...and the value of the word field for all the linked objects in the words array.
Any way how to do this.. i'm thinking about some sort of recursion in checking links but i'm not sure how to implement. another potential problem is going back to the parent layer creating an infinite loop of sorts.
and is it possible through map reduce?
EDIT: clarity.
I'm writing some functions that construct what should be a properly formatted JSON string which I can then parse into a JSON object.
My object is failing on JSONLint with the following error:
syntax error, unexpected TINVALID at line 2
[
SERVER: {
ip = 10.10.10.1,
port = 8100
},
TYPES: [
ssh,
shht
]
]
I assumed that this would give me an array of JavaScript objects.
Even if I did this instead (object instead of array):
{
SERVER: {
ip = 10.10.10.1,
port = 8100
},
TYPES: [
ssh,
shht
]
}
It doesn't work and I get the same error:
I assume that with the object, I would be able to access some data like so:
var serverIP = myObject.SERVER.ip;
That's certainly what I'd like to do.
Many thanks in advance,
Joe
You have to use quotation marks around the identifiers, and the values that are strings:
This is valid JSON:
{
"SERVER": {
"ip": "10.10.10.1",
"port": 8100
},
"TYPES": [
"ssh",
"shht"
]
}
If you are actually not using JSON at all, but just trying to create a Javascript literal object, then you should not use JSONLint to check the code.
This is valid Javascript:
var myObject = {
SERVER: {
ip: '10.10.10.1',
port: 8100
},
TYPES: [
'ssh',
'shht'
]
};
This validates:
{
"SERVER": {
"ip" : "10.10.10.1",
"port" : 8100
},
"TYPES": [
"ssh",
"shht"
]
}
you need double quotes around every string and since its an object you need : instead of =
your mixing json object with javascript arrays
this is json format
{
"item":"value",
"item2":"value"
}
and this would be a JavaScript array
[
"apple",
"orange"
]
os I think this is what you are looking for
{
"SERVER": {
"ip": "10.10.10.1",
"port": 8100
},
"TYPES": [
"ssh",
"shht"
]
};