JSON Payload with Variable String in Python - json

I have tried just about all permutations of quotes
Code 1 below is the hardcoded values even this fails with error 400
loaddata =""" {
"Application":"EssbaseHPA" ,
"db": "PerformanceData",
"jobtype":"dataload",
"Parameters": {
"rule":"PerfromanceData.rul",
"file":"DPerformanceData.txt",
"abortOnError": "false"
}
} """
Code 2 is what I am trying to get to work.
Vars are appname,dbname.datafile
loaddata1= "{"Application": appname ,"db":dbname, "parameters":"{ parameters':"file":datafile,"abortOnError":false"}"}"
The above gives unrecognized token appname
Thanks

You could try it like this:
import json
appname = "hallo"
dbname = "stgres"
a = {
"Application": appname,
"db": dbname,
"jobtype": "dataload",
"Parameters": {
"rule": "PerformanceData.rul",
"file":"DPerformanceData.txt",
"abortOnError": "false"
}
}
json_a = json.dumps(a, indent=4)
print(json_a)

Thanks, gave me an idea on another permutation: Below. I had to build up payload with the vars appname, dbname, datafile below: ldata = "{"application":"" + appname + ""," ldata = ldata + ""db":"" + dbname + "", "jobtype":"dataload"," ldata = ldata + ""parameters": { "file":"" + datafile + ""," ldata = ldata + ""loaddata":"true", "abortOnError":"false"} }" - The key again is the spacing between "parameters": { Need to have a space on both side of the {.
The escaping for " can cause one to go blind.

Related

I want to convert csv to json with key value pair

I have a JSON file as below and want to convert to json with Name key , value pair ,
Eg : "name":"emailAddress",
"value" :"Trest22#gmail.com"
If have multiple JSON tags then it should break into separate tag as shown in image.
Please help
[
{
"emailAddress": "Trest22#gmail.com",
"loginName": "Testinguser222",
"firstName": "Test222",
"surName": "Test22",
"primaryPhone": "",
"companyId": 123445,
"extracompanies": "[12311,33333]",
"middleName": "Test",
"mobilePhone": 6666666666,
"fax": 87687687686
}
]
Want to convert as below
{
{
"name":"emailAddress",
"value" :"Trest22#gmail.com"
}
{
"name":"loginName",
"value":"Testinguser222"
}
{
"name":"firstName",
"value":"Test222"
}
{
"name":"surName",
"value":"Test22"
}
{ "name":"extracompanies",
"value": "[12311,33333]"
}
I am not sure if any library can do that, however, in Java, you can achieve it as shown below,
ObjectMapper mapper = new ObjectMapper();
String test = "{\n" +
" \"emailAddress\": \"Trest22#gmail.com\",\n" +
" \"loginName\": \"Testinguser222\",\n" +
" \"firstName\": \"Test222\",\n" +
" \"surName\": \"Test22\",\n" +
" \"primaryPhone\": \"\",\n" +
" \"companyId\": 123445,\n" +
" \"extracompanies\": \"[12311,33333]\",\n" +
" \"middleName\": \"Test\",\n" +
" \"mobilePhone\": 6666666666,\n" +
" \"fax\": 87687687686\n" +
" \n" +
" }";
Map<String, Object> maps = mapper.readValue(test, Map.class);
List<Map<String,Object>> converted = maps.keySet().stream().map(key -> {
Map<String,Object> internalMap = new HashMap<>();
internalMap.put("name", key);
internalMap.put("value", maps.get(key));
return internalMap;
}).collect(Collectors.toList());
String json = mapper.writeValueAsString(converted);
Output
[{"name":"emailAddress","value":"Trest22#gmail.com"},{"name":"loginName","value":"Testinguser222"},{"name":"firstName","value":"Test222"},{"name":"surName","value":"Test22"},{"name":"primaryPhone","value":""},{"name":"companyId","value":123445},{"name":"extracompanies","value":"[12311,33333]"},{"name":"middleName","value":"Test"},{"name":"mobilePhone","value":6666666666},{"name":"fax","value":87687687686}]

POST users import v2 internal server error

I am working on a fully automatic pipeline for my company where we automatically set up projects, add users and upload files with the different APIs on BIM360. On the stage of adding a user I get a 500 internal server error:
{"code":2000,"message":"no implicit conversion of String into Integer"}
We are using a two-legged authentication approach and as such the header looks like this:
Authorization: Bearer <token> (It has account:write rights)
x-user-id: ************ (uid of my admin account)
Content-Type: application/json
The request content is this:
#"{
""email"": """ + ***#********.** + #""",
""services"": {
""document_management"": {
""access_level"": """ + admin+ #"""
},
""project_administration"": {
""access_level"": """ + admin+ #"""
}
},
""industry_roles"": []}";
I just can't quite seem to figure out what I am doing wrong. Hope someone can help me.
EDIT: Full code for this request
public async static Task<HttpStatusCode> AddUserToProjectEmail(string projectId, string accountId, string accessToken, string userToAddEmail, string userPrivilege, string adminUserId)
{
using (HttpClient httpClient = new HttpClient())
{
using (HttpRequestMessage request = new HttpRequestMessage())
{
//Documentation for what to put in the Http POST: https://forge.autodesk.com/en/docs/bim360/v1/reference/http/projects-project_id-users-import-POST/
request.Method = new HttpMethod("POST");
request.RequestUri = new Uri("https://developer.api.autodesk.com/hq/v2/regions/eu/accounts/" + accountId + "/projects/" + projectId + "/users/import");
//Make the request payload
string jsonPayload = AddPayloadToUserAddEmail(userToAddEmail, userPrivilege);
request.Content = new StringContent(jsonPayload);
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
request.Headers.Add("x-user-id", adminUserId);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
//Send request
var response = await httpClient.SendAsync(request);
return response.StatusCode;
}
}
}
And the request payload method:
private static string AddPayloadToUserAddEmail(string userToAddEmail, string userPrivilege)
{
string payload = #"{
""email"": """ + userToAddEmail + #""",
""services"": {
""project_administration"": {
""access_level"": """ + userPrivilege + #"""
},
""document_management"": {
""access_level"": """ + userPrivilege + #"""
}
},
""industry_roles"": []
}";
return payload;
}
I have checked all the IDs through the URL on BIM360, however it's not possible to check the Uid of my account I think.
EDIT 2: I should note that I was getting a different error before I added the x-user-id header, where it just said forbidden which makes sense. This lead me to think it had something to do with the x-user-id header, but I can't figure it out.
Don't be like me and forget to wrap the payload into an array as stated on the docs that it has to be. Using this as payload worked
#"[{
""email"": """ + userToAddEmail + #""",
""services"": {
""project_administration"": {
""access_level"": """ + userPrivilege + #"""
},
""document_management"": {
""access_level"": """ + userPrivilege + #"""
}
},
""industry_roles"": []
}]";

Extract some value without key from a json object

I am using java to extract a list of node ids from an elastic search running tasks response.
The response looks like this
{
"nodes": {
"ZKUuxQZpRCCcJ0njBM1P0A": {
"name": "ZKUuxQZ",
"transport_address": "127.0.0.1:9300",
"host": "127.0.0.1",
"ip": "127.0.0.1:9300",
"roles": [
"master",
"data",
"ingest"
],
"tasks": {
"ZKUuxQZpRCCcJ0njBM1P0A:118": {
"node": "ZKUuxQZpRCCcJ0njBM1P0A",
"id": 118,
"type": "transport",
"action": "indices:data/write/delete/byquery",
"start_time_in_millis": 1527808643421,
"running_time_in_nanos": 154234724059,
"cancellable": true,
"headers": {}
}
}
}
}
}
In this example, I want to exact ZKUuxQZpRCCcJ0njBM1P0A:118. Can someone give me an example how to extract this information?
Option 1, java json parser. I wanted to write a response class and parse the json string, but ZKUuxQZpRCCcJ0njBM1P0A:118 is not in the key:value format. I am not sure how to extract it.
Option 2, using regex. The node id part may also have - or _. Can someone provide a neat java regex solution? So far my regex is [a-zA-Z0-9\-\_]+\:[0-9]+, I am not sure if this is safe enough.
Thanks.
One way you can do this is by using org.json library and create JSONObject and using keys() you can get all the keys and use this to achieve your task
Add the following dependency in your pom.xml
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
Then you can do something like below:
String jsonString = "{\n" +
" \"nodes\": {\n" +
" \"ZKUuxQZpRCCcJ0njBM1P0A\": {\n" +
" \"name\": \"ZKUuxQZ\",\n" +
" \"transport_address\": \"127.0.0.1:9300\",\n" +
" \"host\": \"127.0.0.1\",\n" +
" \"ip\": \"127.0.0.1:9300\",\n" +
" \"roles\": [\n" +
" \"master\",\n" +
" \"data\",\n" +
" \"ingest\"\n" +
" ],\n" +
" \"tasks\": {\n" +
" \"ZKUuxQZpRCCcJ0njBM1P0A:118\": {\n" +
" \"node\": \"ZKUuxQZpRCCcJ0njBM1P0A\",\n" +
" \"id\": 118,\n" +
" \"type\": \"transport\",\n" +
" \"action\": \"indices:data/write/delete/byquery\",\n" +
" \"start_time_in_millis\": 1527808643421,\n" +
" \"running_time_in_nanos\": 154234724059,\n" +
" \"cancellable\": true,\n" +
" \"headers\": {}\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
JSONObject jsonObject = new JSONObject(jsonString);
Set<String> topLevelKeys = jsonObject.keySet();
for (String key : topLevelKeys) {
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
JSONObject valueObject = jsonObject.getJSONObject(key);
System.out.println(valueObject.toString());
}
}
Extend this as per your requirement.
So regex is too hacky and I figured it out using gson. I wish ElasticSearch can offer us some standard library to extract all kinds of responses. Here is my solution using gson.
import com.google.gson.*;
import org.json.JSONObject;
public class test {
public static void main(String[] args) throws Exception {
String jsonString = "json_format elasticsearch reponse for listing running tasks"
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(content).getAsJsonObject();
jsonObject.getAsJsonObject("nodes").entrySet().forEach(
s -> {
s.getValue().getAsJsonObject().getAsJsonObject("tasks").entrySet().forEach( s2 -> {
System.out.println(s2.getKey());
});
});
}
It prints all the running task ids like the following. It's technically nodeId:taskId, but ES has a very vague description in their task API docs(It says you can use TASK_ID to check a status of a task, well that TASK_ID is nodeId:taskId).
Mw-3i39gTHGxu5c8z9viQQ:503209021
DZ29LMsWR0aW9guWZTYe2Q:482931604
6CAbDZSWR8SfwZgnRT0qNg:494351185
Use the following regex:
[a-zA-Z0-9-_]*[0-9]+[a-zA-Z]+:[0-9]*
You can find the test result here:
https://regexr.com/3qdug

Json Parse Unexpected token with node

I am getting a strange unexpected token error trying to parse a JSON file using node. I have tested the code with 2 files that look identical. I used a code compare tool to do a comparison and they do appear identical. However, when I try to parse them, one gets the error and the other does not. The file that does not work is being generated from a PowerShell script. The one that does work was manually created. I am baffled. One thing I noticed that is different about them when I write the json out to the console is, the one that does not work has a ? at the beginning.
The json from the file that does not work:
data = ?{ "stack_name": "perf-a", "parameters": { "StackSet": "b", "MonitoringEnableAutoscalingAlarm": "True", "MachineConfigEnvironment": "Perf", "AppEnvironmentType": "perf", "StackInRotation": "True", "MonitoringEnableNotificationOnlyAlarms": "False", "AMIImage": "ami-123456789" }, "tags": { "CostCenter": "12345", "Owner": "test#test.com" }, "cft_file":
"cft/cft.json"}
The json from the file that does work:
data = { "stack_name": "perf-a", "parameters": { "StackSet": "a", "MonitoringEnableAutoscalingAlarm": "True", "MachineConfigEnvironment": "Perf", "AppEnvironmentType": "perf", "StackInRotation": "True", "MonitoringEnableNotificationOnlyAlarms": "False", "AMIImage": "ami-123456789" }, "tags": { "CostCenter": "45229", "Owner": "test#test.com" }, "cft_file": "
cft/cft.json"}
The code I am using for testing is:
var envFile = "perf2.json";
var fs = require('fs');
console.log('envFile = ' + envFile);
fs.readFile(envFile, 'utf8', function (err, data) {
if (err) {
console.log('error reading variables file');
throw err;
}
try {
var JsonData = JSON.stringify(data);
console.log('JsonData = ' + JsonData);
data = data.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f")
.replace(/\\0/g, "")
.replace(/\\v/g, "")
.replace(/\\e/g, "\\e");
data = data.replace(/[\u0000-\u001F]+/g, "");
console.log('data = ' + data);
var cftVariables = JSON.parse(data);
console.log('cftVariables = ' + cftVariables);
console.log('cftVariables stack name = ' + cftVariables.stack_name);
} catch (e) {
console.log('error parsing variables file');
throw e;
}
});
As you can see, I have also tried JSON.stringify but I lose the properties and cftVariables.stack_name becomes undefined.
This problem has been plaguing me for several days and I am now at a loss as to how to fix it.
For reference, here is the snippet of PowerShell that creates the file. The problem might be there.
$savePath = "envs/" +$filetouse + ".json"
$parameters = #{AppEnvironmentType =$AppEnvironmentType;
StackSet = $StackSet;
StackInRotation = $StackInRotation;
AMIImage = $amiid;
MonitoringEnableAutoscalingAlarm = $MonitoringEnableAutoscalingAlarm;
MonitoringEnableNotificationOnlyAlarms= $MonitoringEnableNotificationOnlyAlarms;
MachineConfigEnvironment = $MachineConfigEnvironment;
}
$tags = #{Owner = "test#test.com";
CostCenter = "45229";
}
$envcft = #{stack_name =$stack_name;
cft_file = "cft/cft.json";
parameters = $parameters;
tags = $tags;
} | ConvertTo-Json
Write-host("Saving the env file with the new amiId... ")
$envcft | Out-File $savePath -Encoding UTF8 -force
Assuming you are reading the data in from a file, once you have the string you can use the remove function to get rid of that "?".
Something like this:
s = s.replace('?','');
EDIT
since the replace did not work, try 1) either not specifying an encoding when you save the file, or 2) specify UTF16

how to read 3 json responses like one

I have 3 functions that invoke a json response
function one gives:
{
"IT_INFO": {
"CARNET": "999250 ",
"CEDULA": "000013724224",
"NOMBRE": "pedro ",
"SEGNOMBRE": "salomon ",
"APELLIDO": "Perez ",
"SEGAPELLIDO": "Perza ",
"EMAIL": "mail#mailexample.com ",
"IAP": "0.00",
"IAA": "0.00"
}
}
second function :
{
"HISTORICOP": [
{
"MATERIA": "PROCESOS DEL LENGUAJE ",
"NOTA": "7 ",
"ANIO": "2000",
"PERIODO": "001",
"ENEMENOSUNO": "Ordinaria. Estado por defecto "
}
]
}
third function:
{
"HORARIO": [
{
"CODIGO": "BERSP01 ",
"MATERIA": " COMPUTADOR ",
"AULA": "A1-102 ",
"PROFESOR": "Quintero Moros, Nelson ",
"HORARIO": "TU WE FR 08:00-10:00 "
}
]
}
How should it come out so the function JSON.parse(str) will read it?
str = [func1,func2,func3] ??
or
str = [[func1],[func2],[func3]]?
or??? any ideas???
I assume you parse the JSON in JavaScript.
Normally you should not build JSON "manually", but in this case it does not seem to be too bad:
var objs = JSON.parse('[' + [func1(), func2(), func3()].join(',') + ']');
This creates a JSON array with the three objects returned by the functions.
Alternatively you can parse the responses individually:
var objs = [func1(), func2(), func3()];
for(var i = objs.length; i--; ) {
objs[i] = JSON.parse(objs[i]);
}
Of course you have to do things differently if the functions don't return the JSON but make some Ajax request...