I'd like to add JSON object of a json file into another one.
Here is an example :
{
"HOUSES": {
"1": {
"NAME": "House1",
"PEOPLE": {},
"ID": 1
},
"2": {
"NAME": "House2",
"PEOPLE": {},
"ID": 2
},
"3": {
"NAME": "House3",
"PEOPLE": {},
"ID": 3
}
}
}
And people object :
{
"1": {
"NAME": "People1"
},
"2": {
"NAME": "People2"
},
"3": {
"NAME": "People3"
}
}
Now I'd like to add the people object into House1.
I tried that :
var extend = require('util')._extend;
var obj1 = house.PEOPLE; //var content : {}
var obj2 = extend(people, obj1); //var people content people object
This code replace all house1 by the people object. How can I add people object in the sublevel PEOPLE of house1?
I tried with object-assign too, and I have the same problem.
Thanks !
You can do it by:
HOUSES.1.PEOPLE = PEOPLEOOBJ;
Where PEOPLEOBJ is :
PEOPLEOBJ = {
"1": {
"NAME": "People1"
},
"2": {
"NAME": "People2"
},
"3": {
"NAME": "People3"
}
}
Related
I am trying to pull the type JSON data from "0", but I'm getting a SyntaxError. In postman, part of the returned data looks like this:
var array = {
"catalogs": {
"add": {
"0": {
"type": 'catalog',
"id": '2027528',
"date_create": '1637310194',
"date_modify": '1637310194',
"created_by": '7382229',
"modified_by": '7382229',
"catalog_id": '8241',
"name": '',
"deleted": '0',
"custom_fields": {
"0": {
"id": '912743',
"name": 'Статус',
"values": {
"0": {
"value": 'Создан',
"enum": '4197277'
}
},
"code": 'BILL_STATUS'
},
"1": {
"id": '912747',
"name": 'Плательщик',
"values": {
"0": {
"value": {
"name": 'TEST+TEST',
"entity_id": '63836888',
"entity_type": 'contacts'
}
}
},
"code": 'PAYER'
},
"2": {
"id": '912753',
"name": 'Позиции+счета',
"values": {
"0": {
"value": {
"sku": '',
"description": '1111',
"unit_price": '1111',
"unit_type": 'шт',
"quantity": '1',
"discount": {
"type": 'amount',
"value": '0'
},
"vat_rate_id": '0',
"vat_rate_value": '20',
"bonus_points_per_purchase": '0',
"external_uid": ''
}
}
},
"code": 'ITEMS'
},
"3": {
"id": '912755',
"name": 'Комментарий',
"values": {
"0": {
"value": '1111'
}
},
"code": 'BILL_COMMENT'
},
"4": {
"id": '912757',
"name": 'Стоимость',
"values": {
"0": {
"value": '1111'
}
},
"code": 'BILL_PRICE'
}
},
"created_at": '1637310194',
"updated_at": '1637310194'
}
}
},
"account": {
"_links": {
"self": 'https://amocrm.ru'
},
"subdomain": 'mylogoped',
"id": '24809335'
}
};
When I try to use the code, I get an error (SyntaxError: Unexpected number)
var myDat = testOne.catalogs.add.0.type;
Found a solution in one of the posts, but it does not work, an error also comes out
for (let i = 0; i < testOne.catalogs.add.0.length; i++) {
var type = testOne.catalogs.add.0[i].type;
sheet.getRange(lastRow + 1, 5).setValue(type);
}
Found a solution here SyntaxError: Unexpected number when pulling JSON data but it doesn't work because SyntaxError: Unexpected token '['
for (let i = 0; i < testOne.catalogs.add.["0"].length; i++) {
var type = testOne.catalogs.add.["0"][i].type;
sheet.getRange(lastRow + 1, 5).setValue(type);
}
Please tell me how I can take data from the "0" array, everything behind it is pulled like this
var myData = testOne.account.subdomain;
You have no array in the JSON data.
It is an object with nested objects. Also the .["0"] syntax is incorrect.
You could do something like the following to iterate:
var keys = Object.keys(testOne.catalogs.add);//array of keys
for (let i = 0; i < keys.length; i++) {
var type = testOne.catalogs.add[keys[i]].type;
// ...do something
}
But it will not guarantee the order of items to be the same as you see in the data because of how the objects work. If order is important then you can sort the keys array accordingly.
I am trying to access deep-nested lists and dictionaries. I am experimenting with the glom library however my Third_KV key doesn't work on the below JSON object when trying to retrieve the "Country"
from glom import glom
target = {
"Result": {
"Topics": [
{
"A": "abc",
"D": 0,
"Questions": [
{
"E": "jklm",
"P": "dsfs",
"Answers": [
{
"first": "string",
"second": "string",
"Country": "CH"
},
{
"first": "string",
"second": "string",
"Country": "NL"
}
]
}
]
}
]
}
}
path = {
"First_KV": ("Result.Topics", ["Questions"]),
"Second_KV": ("Result.Topics", [("Questions", ["Answers"])]),
"Third_KV": ("Result.Topics", [("Questions", "Answers", ["Country"])])
}
countries = glom(target, path["Third_KV"])
Not very clear what final json/array/structure you want, but without relying on any library, can you not use simple map() e.g.
const jsonTest = {
"Result": {
"Topics": [{
"A": "abc",
"D": 0,
"Questions": [{
"E": "jklm",
"P": "dsfs",
"Answers": [{
"first": "CHfirstCountry",
"second": "CHsecondCountry",
"Country": "CH"
},
{
"first": "NLfirstCountry",
"second": "NLsecondCountry",
"Country": "NL"
}
]
}]
}]
}
};
const AnswersArray = jsonTest.Result.Topics[0].Questions[0].Answers;
let dictPerCountry = new Object();
AnswersArray.map((eachElement) => {
dictPerCountry[eachElement.Country] = [eachElement.first, eachElement.second];
});
console.log({
dictPerCountry
});
dictPerCountry will look like so:
{
"dictPerCountry": {
"CH": [
"CHfirstCountry",
"CHsecondCountry"
],
"NL": [
"NLfirstCountry",
"NLsecondCountry"
]
}
}
Answers are of "list" type too and you are missing its square brackets. check below pattern to get the country
pattern = ('Result.Topics', [('Questions', [('Answers', ['Country'])])])
So you need to change your dictionary "path" to be
path = {
"First_KV": ("Result.Topics", ["Questions"]),
"Second_KV": ("Result.Topics", [("Questions", ["Answers"])]),
"Third_KV": ('Result.Topics', [('Questions', [('Answers', ['Country'])])])
}
I have a sample JSON named SampleJSON as below:
"type1":{
"0":{"title":"Title1","url":"URL1"},
"1":{"title":"Title2","url":"URL2"},
"2":{"title":"Title3","url":"U",
"0":{"title":"Title1","url":"URL1"},
"1":{"title":"Title2","url":"URL2"},
-----------------------------------,
-----------------------------------
}
"3":{"title":"Title4","url":"URL4"},
-----------------------------------,
-----------------------------------
}
How can i loop through the node "type1" in Angular5 and get the value for title & url.
I am stuck and cant move further after trying ngFor.
You can just loop via keys
try this:
https://stackblitz.com/edit/angular-y1wlkt?file=app%2Fapp.component.ts
export class AppComponent {
name = 'Angular 5';
test: any;
testFinal: any[];
constructor() {
this.testFinal = [];
this.test = {
"type1": {
"0": { "title": "Title1", "url": "URL1" },
"1": { "title": "Title2", "url": "URL2" },
"2": {
"title": "Title3", "url": "U",
"0": { "title": "Title1", "url": "URL1" },
"1": { "title": "Title2", "url": "URL2" },
},
"3": { "title": "Title4", "url": "URL4" }
}
}
//Iterate the keys
for(let x in this.test){
for(let y in this.test[x]){
this.testFinal.push(this.test[x][y]);
}
}
console.log(this.testFinal)
}
}
This is my JSON array and how i can find JSON array length.
{"quizlist":[
{
"question":"lion.png",
"option":"lion"
},
{
"question":"tiger.png",
"option":"tiger"
},
{
"question":"cheetah.png",
"option":"cheetah"
},
]}
In your case, it looks like a JS Object already.
So, simply use
var jsObj = {
"quizlist": [{
"question": "lion.png",
"option": "lion"
}, {
"question": "tiger.png",
"option": "tiger"
}, {
"question": "cheetah.png",
"option": "cheetah"
}, ]
}
alert(jsObj.quizlist.length)
I've got an MVC 3 web app and am returning a JSON object which I would like to use Linq against to limit the result returned to the client jquery.
The Json response takes the following structure:
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
....
I need to return or grab just the json items that have a Name like 'Setting' for example. In the example, this would return just the 2 Json nodes.
My Linq is very limited and what I have is: Settings is where the Json response is stored.
NewtonSoft.Json.Linq.JObject data = NewtonSoft.Json.Linq.JObject.Parse(Settings);
var result = from p in data["Data"]["Items"].Children()
where (p["Result"]["Name"].Contains("Expenses.Help.Tip"))
select new { Name = (string)p["Result"]["Name"], Value = (string)p["Result"]["Value"] };
When I run this I get nothing in my result. Can anyone help and tell me what I'm doing wrong?
Thanks.
Well, I'm not a Json specialist, but I think your Json structure has some problems.
I tried an online parser, and parsing took only the third result... (try to copy past your code in the left window, and look at JS eval.
Instead of
"Items": [
{
"Result": {
},
"Result": {
},
"Result": {
}
}]
you should have each element of Items array (each 'Result') into {}.
"Items": [
{
{"Result": {
}
},
{"Result": {
}
},
{"Result": {
}
}]
I got it to work by changing your Json file to
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
using
var data = JObject.Parse(test)["Data"]["Items"].Children()
.Where(m => m["Result"]["Name"].Value<string>().Contains("Setting"))
.Select(m => new
{
Name = m["Result"]["Name"].Value<string>(),
Value = m["Result"]["Value"].Value<int>()
})
.ToList();