How to parse an array from JSON object in NodeJS? - json

I have a JSON data something like:
'1':
{ code: '1',
type: 'car',
},
'2':
{ code: '2',
type: 'bike'
},
...
I dont want to parse 1 and 2 in the parent. Only I need to have
[{code: '1',
type: 'car',
},
{ code: '2',
type: 'bike'
},
...]
How can I do that?

jsonArray = [];
for (var i in JsonData) {
jsonArray.push(jsonData[i]);
}

Related

Remove empty elements from nested JSON

I have a nested json with an arbitrary depth level :
json_list = [
{
'class': 'Year 1',
'room': 'Yellow',
'students': [
{'name': 'James', 'sex': 'M', 'grades': {}},
]
},
{
'class': 'Year 2',
'info': {
'teachers': {
'math': 'Alan Turing',
'physics': []
}
},
'students': [
{ 'name': 'Tony', 'sex': 'M', 'age': ''},
{ 'name': 'Jacqueline', 'sex': 'F' },
],
'other': []
}
]
I want to remove any element that its value meet certain criteria.
For example:
values_to_drop = ({}, (), [], '', ' ')
filtered_json = clean_json(json_list, values_to_drop)
filtered_json
Expected Output of clean_json:
[
{
'class': 'Year 1',
'room': 'Yellow',
'students': [
{'name': 'James', 'sex': 'M'},
]
},
{
'class': 'Year 2',
'info': {
'teachers': {
'math': 'Alan Turing',
}
},
'students': [
{ 'name': 'Tony', 'sex': 'M'},
{ 'name': 'Jacqueline', 'sex': 'F'},
]
}
]
I thought of something like first converting the object to string using json.dumps and then looking in the string and replacing each value that meets the criteria with some kind of flag to filter it after before reading it again with json.loads but I couldn't figure it out and I don't know if this is the way to go
I managed to get the desired output by tweaking this answer a bit:
def clean_json(json_obj, values_to_drop):
if isinstance(json_obj, dict):
json_obj = {
key: clean_json(value, values_to_drop)
for key, value in json_obj.items()
if value not in values_to_drop}
elif isinstance(json_obj, list):
json_obj = [clean_json(item, values_to_drop)
for item in json_obj
if item not in values_to_drop]
return json_obj

Subtract Expression JSON

Update: I am trying to do subtract expression in JSON file.
export const PRODUCTS: Products[] = [
{
id: 1,
productCat:'Product Cat',
product: [
{
productName: 'Product Name',
newPrice: 800,
oldPrice: 1000,
save: 'oldPrice' - 'newPrice'
},
],
}
]
I am new to JSON so I dont know if it's even possible. If there is any other way please help.
Use forEach function to add save key. like this
const PRODUCTS = [{
id: 1,
productCat: 'Product Cat',
product: [{
productName: 'Product Name',
newPrice: 800,
oldPrice: 1000
},
{
name: 'Product Name 2',
newPrice: 1800,
oldPrice: 10000
}
],
}];
PRODUCTS[0].product.forEach(itm => itm['save'] = itm['oldPrice'] - itm['newPrice']);
console.log(PRODUCTS);

How to use React-widget Dropdownlist with groupby attribute

I need to use react-widgets dropdownlist with groupby attribute.
A typical example of this would be;
<Multiselect
data=[{name:'Dan' lastName:'Black'}, {name:'Man' lastName:'Black'}]
textField='name'
groupBy='lastName'
/>
But the data array i have of is of a completely different structure. like;
[
{
name:'test one',
objects: [{key:'my key', value:'my value'},
{key:'my key1', value:'my value1'}
{key:'my key2', value:'my value2'}]
},
{
name:'test two',
objects: [{key:'my key', value:'my value'},
{key:'my key1', value:'my value1'}
{key:'my key2', value:'my value2'}]
}
]
And i need the 'key' to be the value displayed in the list and groupby on 'name'.
Is this possible to achieve or just completely stupid and i have to restructure it totally?
Each objects array is honestly 100 or more objects long.. so i prefer not to restructure it again due to performance issue.
Thanks in advance, All ideas are welcome!
UPDATE:
<DropdownList
data = {
[{
repoName: 'google repository',
objects: [
{
key: 'mykey',
method: 'my meth',
value: 'my val'
},
{
key: 'mykey2',
method: 'my meth2',
value: 'my val2'
}]
}]}
textField='objects.key'
placeholder={placeholder}
groupBy='repoName'
/>
Gives something like;
google repository
[Object Object]
How could iterate that object array to avoid this?
According to my understanding, what you can do is create a different structure for data using your existing one.
const data = [{
name: 'test one',
objects: [{
key: 'my key',
value: 'my value'
}, {
key: 'my key1',
value: 'my value1'
} {
key: 'my key2',
value: 'my value2'
}]
}, {
name: 'test two',
objects: [{
key: 'my key',
value: 'my value'
}, {
key: 'my key1',
value: 'my value1'
} {
key: 'my key2',
value: 'my value2'
}]
}]
const newData = []
data.forEach((element) => {
const name = element.name
if (element.objects && element.object.length) {
element.objects.forEach((keyValueData) => {
newData.push({
name,
key: keyValueData.key,
value: keyValueData.value,
})
})
}
})
< Multiselect
data = {
newData
}
textField = 'name'
groupBy = 'key' / >
This way, it should solve your issue.Mind brackets not tested the code

How to delete an object from a json in angular 2 and ionic

export default [
{
user:{
id: '1',
person: 'Theodore Roosevelt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
},
{
user:{
id: '2',
person: 'Normale',
text: 'Change your thoughts and you change your world.',
icon:'woman'
}
}, {
user:{
id: '3',
person: 'Thlt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
}]
The code above is a ts file under data folder in ionic tool.
I wish to delete an entry from this array on basis of id by click of delete button in front of each entry.
I'm new to ionic . I tried .slice it didn't work
const arr = [{
user:{
id: '1',
person: 'Theodore Roosevelt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
},
{
user:{
id: '2',
person: 'Normale',
text: 'Change your thoughts and you change your world.',
icon:'woman'
}
}, {
user:{
id: '3',
person: 'Thlt',
text: 'Believe you can and you\'re halfway there',
icon:'man'
}
}]
const id = '2'; // 2 will be removed
const result = arr.filter(item => (item.user || {}).id !== id)
console.log(result)

Process JSON using jQuery

I have the following JSON-encoded data being returned and need to process it with jQuery.
How can I access different depots and vehicle data inside this list using jQuery.getJSON()'s callback function?
$.getJSON('url', function(data) {
// ...?
});
The JSON-encoded data:
// top-level result is a list of dictionaries
[
// return dictionary for each depot
{
depot: {
_id: 'D3',
intersection: {
first: 'Bay',
second: 'King'
},
address: {
number: '100',
street: 'King street West',
city: 'Toronto',
province: 'ON',
postal_code: 'M5X 1B8'
},
},
// return dictionary for each car in that depot
vehicle: [{
_id: 'V4',
_depot_id: 'D3',
model: 'Ford F150',
price: '80',
km_per_litre: '15',
cargo_cu_m: 'YES',
category: 'Truck',
image: 'www.coolcarz.com'
}, {
_id: 'V24',
_depot_id: 'D3',
model: 'Toyota Camry Hybrid',
price: '90',
km_per_litre: '25',
cargo_cu_m: 'YES',
category: 'Hybrid car',
image: 'www.coolcarz.com'
}
]
},
{
depot: {
_id: 'D9',
intersection: {
first: 'Bay',
second: 'Front'
},
address: {
number: '161',
street: 'Bay',
city: 'Toronto',
province: 'ON',
postal_code: 'M5J 2S1'
},
},
// return dictionary for each car in that depot
vehicle: [{
_id: 'V11',
_depot_id: 'D9',
model: 'Ford Crown Victoria',
price: '45',
km_per_litre: '13',
cargo_cu_m: 'YES',
category: 'Standard car',
image: 'www.coolcarz.com'
},
]
},
]
$.getJSON('url', function(data) {
alert(data.length); // 2
alert(data[0].depot.intersection.second); // "King"
alert(data[0].vehicle[1].category); // "Hybrid car"
alert(data[1].depot.address.city); // "Toronto"
});