Mapping json string response to pipe

Hi,
I am using openai api to input raw resume data and get the structured resume response data on record creation. I am getting the response as json string rather than json object. When I map the fields in the pipe, I am not getting data in the Tadabase fields individually.
Is there any shortcut to convert the json string to json object or map the fields in the pipe which is json string?

Looking for positive resposne.
Thanks,
Khalid

You didn’t say if you were using the OpenAI Tadabase Pipe, I would have thought that it would (have not tested) returns the values that can then be mapped in Tadabase in the usual way to the individual fields.

Yes, I am using OpenAI Tadabase Pipe.
I am getting the response string as following in choices.0.message.content in response and it is getting into the Tadabase record through pipe:
{
“name”: “John Doe”,
“email”: “johndoe@email.com”,
“phone number”: “(123) 456-7890”,
“degree”: “Bachelor of Science in Computer Engineering”,
“institution”: “University of Tech”,
“graduation year”: “2016”,
“company”: [“XYZ Corp”, “ABC Technologies”],
“position”: [“IT Systems Engineer”, “Software Engineer”],
“duration”: [“June 2020 - Present”, “Jan 2017 - May 2020”],
“technical skills”: [“Programming Languages: Python, Java, C++”, “Frameworks: Django, Flask, React”, “Databases: MySQL, PostgreSQL, MongoDB”, “Cloud Technologies: AWS, Azure”, “Version Control: Git, GitHub, GitLab”, “Tools: Docker, Kubernetes, Jenkins”],
“languages”: “N/A”
}

If you see the screenshot, value is not received in choices.0.message.content.name and choices.0.message.content.degree etc, fields.
Am I doing something wrong in mapping fields?

I am mapping the name and degree as in Text field. Full response object I am receiving as following:

{
    "id": "chatcmpl-AITP5R81fxRx5fk5Wp3VCsfdf231",
    "object": "chat.completion",
    "created": 1728966295,
    "model": "gpt-4-0613",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "{\n  \"name\": \"John Doe\",\n  \"email\": \"johndoe@email.com\",\n  \"phone number\": \"(123) 456-7890\",\n  \"degree\": \"Bachelor of Science in Computer Engineering\",\n  \"institution\": \"University of Tech\",\n  \"graduation year\": \"2016\",\n  \"company\": [\"XYZ Corp\", \"ABC Technologies\"],\n  \"position\": [\"IT Systems Engineer\", \"Software Engineer\"],\n  \"duration\": [\"June 2020 - Present\", \"Jan 2017 - May 2020\"],\n  \"technical skills\": [\"Programming Languages: Python, Java, C++\", \"Frameworks: Django, Flask, React\", \"Databases: MySQL, PostgreSQL, MongoDB\", \"Cloud Technologies: AWS, Azure\", \"Version Control: Git, GitHub, GitLab\", \"Tools: Docker, Kubernetes, Jenkins\"],\n  \"languages\": \"N/A\"\n}",
                "refusal": null
            },
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 497,
        "completion_tokens": 186,
        "total_tokens": 683,
        "prompt_tokens_details": {
            "cached_tokens": 0
        },
        "completion_tokens_details": {
            "reasoning_tokens": 0
        }
    },
    "system_fingerprint": null
}

Thanks,
Khalid Naseem

The data inside the “content” is an escaped json string. The pipe can’t extract a value from within that. You can see that because it has lots of backslashes. As far as the Pipe is concerned, the entire value is one string.

What you’d need to do is:

  1. Save the full ‘content’ to a Rich Text field.
  2. Use custom code JavaScript code to extract a specific value from it.

Alternatively, you can prompt chatgpt to just respond with a specific value instead of a JSON.

Suppose I have 2 fields, in the first field I’d save the full content. In the second field, I’d run a table rule to extra just the value that I want.

I assume you know how to save the full content, next install the Custom Javascript Pipe and update it as follows:

Use this code in the Request tab:

function extractValueFromJson(jsonString, key) {
  let jsonObject = jsonString;

  // Check if the input is a string and parse only if necessary
  if (typeof jsonString === 'string') {
    try {
      jsonObject = JSON.parse(jsonString);
    } catch (error) {
      console.error('Invalid JSON string:', error);
      return null;
    }
  }

  return jsonObject[key];
}

const jsonString = {myChatGPTResponse};
extractValueFromJson(jsonString , 'Degree');

In the last line, change it to the key of whatever you want to extract (case sensitive).

Then set a table rule to pass the JSON that you saved earlier and get the "Response value back into another field.

I hope that helps.

1 Like

Thanks Moe,
It was great help for me.

I did some change in your javascript function for rule 2 and rather than returning value of individual field, I am returning json object and mapping the response field, which is working fine for all the field except company, position, duration and technical skills as they are the arrays.
These four field are capturing only first record of each arrays in the Tadabase table.

Here is the Modified function:
function extractValueFromJson(jsonString) {
let jsonObject = jsonString;

// Check if the input is a string and parse only if necessary
if (typeof jsonString === ‘string’) {
try {
jsonObject = JSON.parse(jsonString);
} catch (error) {
console.error(‘Invalid JSON string:’, error);
return null;
}
}

return jsonObject;
}

const jsonString = {myChatGPTResponse};

extractValueFromJson(jsonString);

Response I am getting as following:

{
“status”: “success”,
“result”: {
“name”: “John Doe”,
“email”: “johndoe@email.com”,
“phone number”: “(123) 456-7890”,
“degree”: “Bachelor of Science in Computer Engineering”,
“institution”: “University of Tech”,
“graduation year”: “2016”,
“company”: [
“XYZ Corp”,
“ABC Technologies”
],
“position”: [
“IT Systems Engineer”,
“Software Engineer”
],
“duration”: [
“June 2020 - Present”,
“Jan 2017 - May 2020”
],
“technical skills”: [
“Programming Languages: Python, Java, C++”,
“Frameworks: Django, Flask, React”,
“Databases: MySQL, PostgreSQL, MongoDB”,
“Cloud Technologies: AWS, Azure”,
“Version Control: Git, GitHub, GitLab”,
“Tools: Docker, Kubernetes, Jenkins”
],
“languages”: “N/A”
},
“expression”: “function extractValueFromJson(jsonString) {\n let jsonObject = jsonString;\n\n // Check if the input is a string and parse only if necessary\n if (typeof jsonString === ‘string’) {\n try {\n jsonObject = JSON.parse(jsonString);\n } catch (error) {\n console.error(‘Invalid JSON string:’, error);\n return null;\n }\n }\n\n return jsonObject;\n}\n\nconst jsonString = { "name": "John Doe", "email": "johndoe@email.com", "phone number": "(123) 456-7890", "degree": "Bachelor of Science in Computer Engineering", "institution": "University of Tech", "graduation year": "2016", "company": ["XYZ Corp", "ABC Technologies"], "position": ["IT Systems Engineer", "Software Engineer"], "duration": ["June 2020 - Present", "Jan 2017 - May 2020"], "technical skills": ["Programming Languages: Python, Java, C++", "Frameworks: Django, Flask, React", "Databases: MySQL, PostgreSQL, MongoDB", "Cloud Technologies: AWS, Azure", "Version Control: Git, GitHub, GitLab", "Tools: Docker, Kubernetes, Jenkins"], "languages": "N/A" };\n\nextractValueFromJson(jsonString);”
}

Response mapping is as following:

Now, I removed the .0 from the following field to get the array items. It worked.

Thanks,
Khalid

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.