Add key-value pair in exiting json object in nunjucks - node.js

I want to add a new key-value pair in JSON object with nunjucks template, but unable to add it(don't want to add any custom filter).
suppose I have an object
var obj = {"id": "1", "name": "test", "child": {"id": "2", "status": "true"},
"template": "{{obj.child}}"}
Now I want to add a new key like
{"template": "{% set json = obj.child %}{% set json.name = 'testing' %}"}
So the final output as I want
{
"id": "1",
"name": "test",
"child": {
"id": "2",
"status": "true"
},
"template": {
"id": "2",
"status": "true",
"name": "testing"
}
}
Please let me how can I do?

Related

Parse a json data out from a tag

The text below is part of a string I parsed from html (pre). Since I can not place tags <> in this box I have replace the beginning and end tags as (pre) (/pre).
(pre)(b)Below are the details for server SomeServerName from NetSystem.(Data Length - 1)(/b)
[
{
"askId": "Value1",
"billingCode": "99999999",
"clusterId": null,
"createdBy": "Mike",
"createdFromLegacy": null,
"createdOn": "2021-08-06T17:54:28.220Z",
"description": "Windows 2019",
"environment": "devops",
"hostId": null,
"id": "acd16582-b009-4667-aa95-5977603772sa",
"infrastructure": {
"apiId": "App2019_SA_1_v8-w2019_mike3_cc8f7e02-d426-423d-addb-b29bc7e163be",
"capacityId": "ODI",
"catalogManagementGroup": "Sales Marketing"
},
"legacyId": "XL143036181",
"location": {
"availabilityZone": "ny",
"code": "mx31",
"description": "uhg : mn053",
"region": "west",
"vendor": "apple"
},
"maintenance": {
"group": "3",
"status": "steady_state",
"window": {
"days": "Sunday",
"endTime": "06:00:00.000Z",
"startTime": "02:00:00.000Z"
}
},
"name": "SomeServer",
"network": {
"fqdn": "SomeServer.dom.tes.contoso.com",
"ipAddress": "xx.xx.xx.xx"
},
"os": {
"description": "Microsoft windows 2019",
"type": "windows",
"vendor": "Microsoft",
"version": "2019"
},
"owner": {
"id": "000111111",
"msid": "jtest"
},
"provision": {
"id": "ba424e42-a925-49a5-a4b7-5dcf41b69d4e",
"requestingApi": "mars Hub",
"system": "vRealize"
},
"specs": {
"cpuCount": 4,
"description": "Virtual Hardware",
"ram": 64384,
"serialNumber": null
},
"status": "ACTIVE",
"support": {
"group": "Support group"
},
"tags": {
"appTag": "minitab"
},
"updatedBy": "snir_agent",
"updatedOn": "2021-08-06T17:54:31.525Z"
}
](/pre)
As you can see this is almost json data but I can not parse it as such because of the (b) (/b) tag that exists inside my (pre) (/pre) tag. How can I parse out this (b) tag with its content so I am left with the json data and can treat it as such enabling me to more easily select values with json functions.
If your JSON always has [] brackets you can extract the content inside it and then parse it:
Python example:
import re
import json
text = '<b>asd</b>[{"a": "b", "c": "d"}] pre' # your content
json_content = re.findall('\[[^\]]*\]', text)[0] # '[{"a": "b", "c": "d"}]'
result = json.loads(json_content) # [{'a': 'b', 'c': 'd'}]
You can do this either by using re as indicated here or using split:
cleaned = data.split("(b)")[0] + data.split("(/b)")[1]
Above line will concatenate the content before (b) and after (/b) cleaning the b tag and its content.

Azure API Set-body JSON to JSON covert

The response i am getting is below Which i need to convert the input JSON Format to other JSON structure and send the response back. I am struck how to get the data from the JSOn and construct the new JSON format
{
"totalSize": 1,
"done": true,
"records": [{
"attributes": {
"type": "test123",
"url": "/services/data/testapp"
},
"Id": "8373837",
"Name": "6294",
"Application": "9932932932",
"contact": {
"attributes": {
"type": "testcon",
"url": "/services/data/testappsss"
},
"Name": "testName",
"FirstName": "test",
"LastName": "name",
"MailingStreet": null,
"MailingCity": null,
"unemail": "testname#test,.co",
"MailingState": null,
"MailingCountry": null,
"MailingPostalCode": null,
"stuId": "328237832"
},
"currentusbss": "83277832873278",
"currentsu": {
"attributes": {
"type": "testsub",
"url": "/services/data/v44.0jsjsj"
},
"price": 2,
"Name": "SUB-20426"
},
"bal": 234,
"startdate": "2020-02-03",
"enddate": "2020-05-03"
}]
}
I need to convert above JSON format to below JSON format and send it using set-body method in out-bond policies
{
"info": {
"studentName": "testName",
"studentFirstName": "test",
"studentMiddleName": "",
"studentLastName": "Name",
"studentEmail": "testname#test,.co",
"role": "STUDENT",
"billingCountryCode": "US",
"systemId": "XX",
"stuId": "328237832"
},
"address": {
"address1": "1234 Grove St",
"address2": "",
"city": "Tempe",
"countryCode": "US",
"countryDescription": "UNITED STATES",
"stateCode": "AZ",
"stateDescription": "Arizona",
"postalCode": "45235",
"foreignState": "Arizona",
"region": "Domestic",
"phoneNumber": ""
},
"account": {
"institutionId": "1",
"paymentPlan": "N",
"currencyDesc": "United States Dollars",
"currencyType": "USD",
"bal": 234,
"daysLate":"18",
"opportunityId": "9932932932",
"studentParameterName": null,
"studentParameterValue": null
},
"studentTerms": [
{
"startdate": "2020-02-03",
"enddate": "2020-05-03",
"Name": "SUB-20426",
"description": "XQYember 03, 2020 "
}
]
}
You can use Liquid Template for this case:
Using Liquid Templates in Azure API Management
Using Liquid templates with set body
Or you create a new body in the outbound-section with a new JObject

In below JSON, I am able to set the key in the cookies by cookies.set("key").... But how to get values from cookies such as name, email and grade

key:
{
"token": "asasdnmsdbmbfdmnbfmfb",
"userData": {"id": "2", "name": "carrot", "email": [{"abc#google.com"}],
"grade": ["a","b"]
}
How to get name and email and gradefrom cookies.
Your error is [{"abc#google.com"}]. It's not a JSON format. It must be ["abc#google.com"]
You can test this
{"token": "asasdnmsdbmbfdmnbfmfb", "userData": {"id": "2", "name": "carrot", "email": ["abc#google.com"], "grade": ["a","b"]}}
const input = {
"token": "asasdnmsdbmbfdmnbfmfb",
"userData": {"id": "2", "name": "carrot", "email": ["abc#google.com"]},
"grade": ["a","b"]
}
console.log(input.userData.name);
console.log(input.userData.email[0]);
console.log(input.grade);

Retrofit: Expected BEGIN_ARRAY but was BEGIN_OBJECT

As I do for the get object "services" ?
{
"success": "true",
"code": "200",
"message": "The operation was successful",
"data": {
"hairdressers": [
{
"name": "Jason Grant",
"image_profile": "http:\/\/www.kbapi.co\/images\/header\/3.png",
"open": "09:00:00",
"close": "15:00:00",
"id": "2",
"id_saloon": "4",
"services": [
{
"name": "Corte",
"price": "8000",
"image": "http:\/\/www.kbapi.co\/images\/header\/1.png",
"time": "01:00:00",
"type": "W",
"saloon_id": "4",
"service_id": "3"
},
{
"name": "Corte",
"price": "8000",
"image": "http:\/\/www.kbapi.co\/images\/header\/1.png",
"time": "01:00:00",
"type": "C",
"saloon_id": "4",
"service_id": "4"
},
{
"name": "Corte",
"price": "9000",
"image": "http:\/\/www.kbapi.co\/images\/header\/1.png",
"time": "01:00:00",
"type": "O",
"saloon_id": "4",
"service_id": "5"
}
]
},
{
"name": "Herbert Davis",
"image_profile": "http:\/\/www.kbapi.co\/images\/header\/4.png",
"open": "10:00:00",
"close": "16:00:00",
"id": "3",
"id_saloon": "4",
"services": []
}
]
}
}
The error occurs because you're telling Retrofit that you're expecting a JSONArray but instead you're getting a JSON object. I took a quick look at the result that you're using and it looks like it returns a JSON object and the returned object then contains the list you're interested in accessing. I can help you further if you post your GET call.
You should wrap your list of objects with one more java class
public class Response {
private boolean success;
private int code;
private String message;
private List<Data> data;
}
public class Data {
private List<Hairdresser> hairdressers = new ArrayList<Hairdresser>();
}
to generate this objects automatically you can use service like this one

Need to delete a particular block

From file,
====================================
{
"id": "ffc131ff-1793-4109-940f-5b537f7061cf",
"securityResourceId": "48d0eeff-690d-4c2c-b6f9-9b25315f9ca3",
"name": "Dev-bpimdmgr-idev3-01",
"active": true,
"licensed": true,
"licenseType": "AUTHORIZED",
"status": "ONLINE",
"tags": []
},
{
"id": "82db2888-7a2f-48fe-bc25-26a5e28bb340",
"securityResourceId": "5a437865-6ced-402e-ac47-dd38191e5696",
"name": "obiee-cmixdmgr-nprd3-01",
"active": true,
"licensed": true,
"licenseType": "AUTHORIZED",
"status": "ONLINE",
"tags": [
{
"id": "fbf62944-a8a4-4a22-8e75-cd8d88eacaff",
"name": "obiee-tag",
"color": "#32cd32",
"description": "obiee tag for version import",
"objectType": "Agent"
}
]
},
I want to delete tags[] block including where are there inside this block. through perl or shell script
Regards,
Kalaiyarasan
Just use the JSON module:
use JSON qw{ from_json to_json };
my $struct = from_json("[$input]");
delete $_->{tags} for #$struct;
print to_json($struct);

Resources