How to edit the JSON structure - json

I am having a JSON object like below:
{
"ItemIsExportControlled": [
"true",
"false"
],
"OrderShippingDestination": [
"domestic",
"foreign"
],
"CustomerCreditStatus": [
"approved",
"denied",
"unknown"
],
"OrderShipping": [
"ground",
"air",
"sea"
],
"ItemStockStatus": [
"validInStock",
"invalid",
"validOutOfStock"
],
"OrderDeliveryTimeframe": [
"oneMonth",
"immediate",
"oneWeek"
],
"OrderPricingScheme": [
"scheme2",
"scheme1",
"scheme3"
]
}
I am getting two values from onChange event in variables say item and value, so what I need to do is to compare item with this JSON structure keys, and if it matches, then I want to replace the values of that corresponding key with the new value that I got in value variable.
For e.g. If item is ItemIsExportControlled and value is false. Then I want my resulting JSON structure to be
{
"ItemIsExportControlled": [
"false"
],
"OrderShippingDestination": [
"domestic",
"foreign"
],
"CustomerCreditStatus": [
"approved",
"denied",
"unknown"
],
"OrderShipping": [
"ground",
"air",
"sea"
],
"ItemStockStatus": [
"validInStock",
"invalid",
"validOutOfStock"
],
"OrderDeliveryTimeframe": [
"oneMonth",
"immediate",
"oneWeek"
],
"OrderPricingScheme": [
"scheme2",
"scheme1",
"scheme3"
]
}
Can anyone please help me on this. Thanks in advance..

Its simple lets say your JSON is :
const YOUR_JSON = {
"ItemIsExportControlled": [
"true",
"false"
],
"OrderDeliveryTimeframe": [
"oneMonth",
"immediate",
"oneWeek"
],
"OrderPricingScheme": [
"scheme2",
"scheme1",
"scheme3"
]
}
And this could be your onChange function where you are getting item and value
onChange (item, value) {
const result = {...YOUR_JSON, [item]: [value]};
}
You will have updated JSON in result

Related

How to get the key and values of a json string based on a string in the key

I have a JSON file that look like the following:
{
"rooms":{
"operatorLimbo":[
],
"pager":[
"w0vu9ourxkdpkf0vwosrim",
"mekkgn9e9kdpwf2rzpkf41",
"upkicevngkd5s2h8o01wyz",
"x29ywtag4kfs0mylq2rd4v"
],
"ru_pagers":[
"mekkgn9e9kdpwf2rzpkf41"
],
"ch_pagers":[
"upkicevngkd5s2h8o01wyz"
],
"operators":[
"r9agylhvekcoibhrk8db8c",
"p94jb2ocyk5hv0ch8tivk5",
"v7cxhqxa3kdgik1h4t1sx0",
"spuvkm9nnketwnqeelqzbf"
],
"pl_operators":[
"spuvkm9nnketwnqeelqzbf"
],
"clients":[
"g99g943hlkh0kp1864rwpz",
"prky4witjkh0naaswiewzs"
],
"pl_users":[
],
"ru_operators":[
"r9agylhvekcoibhrk8db8c"
],
"ch_operators":[
"v7cxhqxa3kdgik1h4t1sx0"
],
"nl_operators":[
"p94jb2ocyk5hv0ch8tivk5"
],
"nl_pagers":[
"w0vu9ourxkdpkf0vwosrim"
],
"ch_users":[
"prky4witjkh0naaswiewzs"
],
"nl_users":[
"g99g943hlkh0kp1864rwpz"
],
"pl_pagers":[
"x29ywtag4kfs0mylq2rd4v"
],
"ru_users":[
],
"us_operators":[
],
"au_operators":[
],
"au_users":[
],
"au_pagers":[
],
"za_operators":[
],
"yoiqfvvvokh0kp2v53sic9":{
"roomid":"yoiqfvvvokh0kp2v53sic9",
"type":"1to1",
"ujid":"g99g943hlkh0kp1864rwpz",
"aid":"3173aaacd43941c7bef1a99e0057ba2b",
"operator":{
"fullname":"Operator",
"name":"Operator",
"lastname":"Jura"
},
"members":[
"Ss0G3RCClvOO8I03AAIp",
"jZGZ_yAb-SC3bEpmAAJz"
],
"chatRecord":11151,
"ojid":"p94jb2ocyk5hv0ch8tivk5",
"usid":"jZGZ_yAb-SC3bEpmAAJz"
},
"deomjtr27kh0l52ogg9zxh":{
"roomid":"deomjtr27kh0l52ogg9zxh",
"type":"1to1",
"ujid":"prky4witjkh0naaswiewzs",
"aid":"3173aaacd43941c7bef1a99e0057ba2b",
"operator":{
"fullname":"Operator",
"name":"Operator",
"lastname":"Jura"
},
"members":[
"DPeOVb1P6qCVXO6GAAJi",
"AAkb84mpMxLzFHklAAJ7"
],
"chatRecord":11153,
"ojid":"v7cxhqxa3kdgik1h4t1sx0",
"usid":"AAkb84mpMxLzFHklAAJ7"
}
}
}
What I'm interested in is to get values like ch_operators and pl_operators and so on basically all entries that have _operators in it and also the value of it,
with the following code I can get just the keys, how can I get the values as well,
Object.keys(this.rooms).filter((key) => key.includes("_operators"))
any help would be appreciated.
You can use a combination of Object.entries, .filter and Object.fromEntries to create a new object with selected properties:
Object.fromEntries(
Object.entries(this.rooms).filter(([key]) => key.includes("_operators"))
)
Object.entries returns an array containing both key and value (as "tuple"), and Object.fromEntries builds a new object from such an array.

How can i insert object which got arrays into arrays using jq?

I may not discribed it properly, I tried many times and looked up the manaual on jq Manaual , and i got no idea how to insert object which contains array into a json file by jq command, anyway, here is the origin test.json:
[
{
"gate": [
{
"pro1": "1"
}
],
"home": [
{
"mem1": "1"
}
],
"holder": "1"
}
]
And i wish to be like this after insert:
[
{
"gate": [
{
"pro1": "1"
}
],
"home": [
{
"mem1": "1"
}
],
"holder": "1"
},
{
"gate": [
{
"pro1": "2"
}
],
"home": [
{
"mem1": "2"
}
],
"holder": "1"
}
]
Could it possibly done by jq?
Since you haven't provided more detail I am going to assume that you simply want to append a hard-coded object to an existing array. (If that's not what you mean you'll need to be more precise in your question.)
You can add items to the end of a list with the + operator. So in your case:
jq '.+[{"gate":[{"pro1": "2"}], "home":[{"mem1": "2"}],"holder": "1"}]' input.json
[
{
"gate": [
{
"pro1": "1"
}
],
"home": [
{
"mem1": "1"
}
],
"holder": "1"
},
{
"gate": [
{
"pro1": "2"
}
],
"home": [
{
"mem1": "2"
}
],
"holder": "1"
}
]
The . takes the existing input, which is an array. The +[ {...} ] concatenates it with another array of one object. If you want to put the new item(s) at the start instead of the end, swap it round: [ {...} ]+.

How to save data in Json format in React-admin v3?

I created JsonInput, which sends a json object to the input SimpleForm, however, a modified version of it gets into the Data Provider.
that's what is sent to the input:
json: {
"FunctionalGroup": [
{
"uaIDref": [
"2104"
],
"_Name": "Текущие параметры",
"_ID": "33"
},
{
"uaIDref": [
"2100"
],
"_Name": "Текущие параметры пониженной точности",
"_ID": "34"
},
],
"_Name": "Прибор 1",
"_ID": "32"
}
that's what came to the server:
json: {
"FunctionalGroup": [
{
"uaIDref": [
"2104"
],
"_Name": "Текущие параметры",
"_ID": "33"
},
{
"uaIDref": [
"2100"
],
"_Name": "Текущие параметры пониженной точности",
"_ID": "34"
}
],
"FunctionalGroupIds": "",
"_Name": "Прибор 1",
"_ID": "32"
},
after a response from the server with this object, this is what came in the field:
json: {
FunctionalGroup: [
{
uaIDref: [
'2104'
]
},
{
uaIDref: [
'2100'
]
}
],
FunctionalGroupIds: ''
}
what kind of magic is this?
I did some tests. When I converted the object to a string, the correct json was sent to the server, however, in the field after the server responded, the modified object was displayed again.
I found out that the conversion is related to redux.js, but what exactly happens is not understood.
Please give any comments on this.
How can I make save json?

How to store a json value in a postgres column

I have a table with two columns,One column should store int and other should store json.
here the data which i want to store in the table.
id,polygon
1,"{""type"": ""Feature"",
""properties"": {
""stroke"": ""#555555"",
""stroke-width"": 2,
""stroke-opacity"": 1,
""fill"": ""#00aa22"",
""fill-opacity"": 0.5
},
""geometry"": {
""type"": ""Polygon"",
""coordinates"": [
[
[-76.97021484375,
40.17887331434696
],
[-74.02587890625,
39.842286020743394
],
[-73.4326171875,
41.713930073371294
],
[-76.79443359375,
41.94314874732696
],
[-76.97021484375,
40.17887331434696
]
]
]
}
}"
I tired storing in postgres as follows:
insert into gjl_polygon values(1,'"{""type"":
""Feature"",""properties"": {""stroke"": ""#555555"",""stroke-
width"": 2,""stroke-opacity"": 1,""fill"": ""#00aa22"",""fill-
opacity"": 0.5},""geometry"": {""type"":
""Polygon"",""coordinates"":
[[[-76.97021484375,40.17887331434696],[-74.02587890625,
39.842286020743394 ],[-73.4326171875, 41.713930073371294],
[-76.79443359375,41.94314874732696],
[-76.97021484375,40.17887331434696]]]}}"');
I got the following error,
Expecting ':' delimiter: line 1 column 4 (char 3)
The problem of your code is the use of double quotes twice. Try to edit like this:
{
"type": "Feature",
"properties": {
"stroke": "#555555",
"stroke-width": 2,
"stroke-opacity": 1,
"fill": "#00aa22",
"fill-opacity": 0.5
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-76.97021484375,
40.17887331434696
],
[-74.02587890625,
39.842286020743394
],
[-73.4326171875,
41.713930073371294
],
[-76.79443359375,
41.94314874732696
],
[-76.97021484375,
40.17887331434696
]
]
]
}
}
The JSON above is a valid JSON string and it should work as expected.

Drawing Polygons on Google Maps using AngularJS and JSON stored coordinates

I have multiple JSON files (poly1.json, poly2.json) with the following setup:
{
"Polygon1": {
"name": "poly1",
"specifications": [
{
"areaGeometry": {
"type": "Polygon",
"coordinates": [
[
[
5.129820025,
52.085407733
],
[
5.129117875,
52.086181679
],
[
5.128497179,
52.087946286
],
[
5.128458022,
52.088253322
],
[
5.12866837,
52.088507157
],
[
5.129251266,
52.088976802
],
[
5.129473861,
52.08926905
],
[
5.129385309,
52.089499203
],
[
5.12909759,
52.089698198
],
[
5.127961124,
52.090148712
],
[
5.127685173,
52.090462912
],
[
5.127310682,
52.091653473
],
[
5.12710699,
52.092271708
],
[
5.127126612,
52.092518366
],
[
5.128237531,
52.093468305
],
[
5.128130926,
52.093688728
],
[
5.126525853,
52.094399058
],
[
5.126377274,
52.09459342
],
[
5.126284571,
52.095035437
],
[
5.130996578,
52.095312264
],
[
5.137138625,
52.095591962
],
[
5.139036247,
52.095628598
],
[
5.138962372,
52.095484813
],
[
5.137879856,
52.093651573
],
[
5.137480747,
52.093048367
],
[
5.136997815,
52.092468872
],
[
5.13643473,
52.091917507
],
[
5.135795776,
52.091398471
],
[
5.134288171,
52.090401311
],
[
5.133608279,
52.089984575
],
[
5.133259679,
52.089768435
],
[
5.132932239,
52.089549796
],
[
5.132656508,
52.089342179
],
[
5.132411195,
52.089120552
],
[
5.132198186,
52.088886599
],
[
5.132143714,
52.088818019
],
[
5.130950838,
52.087097103
],
[
5.130737143,
52.086736442
],
[
5.130575274,
52.086365674
],
[
5.130403794,
52.085570404
],
[
5.129783706,
52.08522338
],
[
5.129820025,
52.085407733
]
]
]
}
}
],
}
}
Now I want to draw all the polygons on Google Maps on where the user is looking at -if there is a polygon in his screen. What is the best way to do so? Should I use for each or are there better ways? How do you iterate over it? Would that not make it slow?
According to my understanding, there are two ways you can go about it. First, you can make a KML layer. You can put together all these polygons in one KML file and make an overlay and integrate that overlay with your map object. (Here's the JQuery reference)
Another way to do it is by using your JSON files and making a data layer. I would suggest to use the drag and drop GeoJSON code example as a reference. Because that will give you accuracy to the specific points. But you can always go for the simpler implementation, you can find other sample codes from Google on the left side of this link.