Skip to main content

Rename Key based on other key's value

Input Data with multiline json
{
                "attributeid": 212077,
                "attributename": "SKU",
                "attributevalue": "999898",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
            {
                "attributeid": 212078,
                "attributename": "Product Name",
                "attributevalue": "Pim Item Update Testing",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
Below is the pycode ops
keyname = ["attributename"]
keyvalue = ["attributevalue"]
for i in keyname:
    attributename = _data.pop(i, None)
for j in keyvalue:
    attributevalue = _data.pop(j, None)
_data[attributename] = attributevalue
_data.update({"new_key":"new_value"})

Note: In the above example as we want to do some manipulation in the incoming data so for that we have used _data variable as this _data holds our incoming data and based on that we can do the manipulation in our data using pycode script.

Output data after applying Python Pycode
{
                "attributeid": 212077,
                "SKU": "999898",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
            {
                "attributeid": 212078,
                "Product Name": "Pim Item Update Testing",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
 
Input Data with multiline json

 

{
                "attributeid": 212077,
                "attributename": "SKU",
                "attributevalue": "999898",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false"
            }
Below is the pycode ops

Responsedata = []
import datetime
import random
import string
import hashlib
import base64
import hmac
import urllib.parse
request_method = 'POST'
url = 'https://xxxxxxxxxx.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql'
oauth_consumer_id = 'xxxxxxxxxx'
oauth_consumer_key = 'xxxxxxxxxx'
oauth_consumer_secret ='xxxxxxxxxx'
oauth_token ='xxxxxxxxxx'
oauth_token_secret ='xxxxxxxxxx'
oauth_signature_method = 'HMAC-SHA256'
oauth_timestamp = str(int(datetime.datetime.now().timestamp()))
oauth_nonce = ''.join(random.choices(string.ascii_letters + string.digits, k = 11))
oauth_version = '1.0'
normalized_request_method = request_method.replace(' ', '')
normalized_string_url = urllib.parse.quote(url, safe = '')
normalized_params = {'oauth_consumer_key': oauth_consumer_key,'oauth_token': oauth_token,'oauth_signature_method': oauth_signature_method,'oauth_timestamp': oauth_timestamp,'oauth_nonce': oauth_nonce,'oauth_version': oauth_version}
sorted_params = dict(sorted(normalized_params.items()))
normalized_string_parmas = [k+'='+v for k,v in sorted_params.items()]
normalized_string_parmas = '&'.join([str(elem) for elem in normalized_string_parmas])
normalized_string_parmas.replace(' ','')
normalized_string_parmas = urllib.parse.quote(normalized_string_parmas, safe = '')
base_string = request_method + '&' + normalized_string_url + '&' + normalized_string_parmas
base_string = str.encode(base_string)
signature_key = oauth_consumer_secret + '&' + oauth_token_secret
signature_key = str.encode(signature_key)
oauth_signature = hmac.new(signature_key, base_string, hashlib.sha256)
oauth_signature.hexdigest()
oauth_signature = base64.b64encode(oauth_signature.digest())
oauth_signature = oauth_signature.decode('UTF-8')
oauth_signature = urllib.parse.quote(oauth_signature, safe = '')
signature ='OAuth realm="'f'{oauth_consumer_id}",oauth_consumer_key="'f'{oauth_consumer_key}",oauth_token="'f'{oauth_token}",oauth_signature_method="'f'{oauth_signature_method}",oauth_timestamp="'f'{oauth_timestamp}",oauth_nonce="'f'{oauth_nonce}",oauth_version="'f'{oauth_version}",oauth_signature="'f'{oauth_signature}"'

Responsedata.append({"authorization":signature})

Note: In the above example as we want to add the signature generated by pycode script in the incoming data so for that we have used Responsedata variable as an empty array [] this Responsedata holds our response from the pycode script and the same will be added in our incoming data.

Output data after applying Python Pycode
{
                "attributeid": 212077,
                "attributename": "SKU",
                "attributevalue": "999898",
                "attribute_groupid": 24315,
                "attribute_groupname": "Default",
                "Isvariant": "false",
                "items": [{"authorization":signature}]
            }