3

I have a JSON File with key value pairs and I want to put the key value pairs into headers. So when I have a file with content like this:

[{"msgId": "8600C5A3-C666-4E63-BFDB-52BCF557F938", "jiraId": "ERR002"}]

I want to create headers with the name msgId and with value "8600C5A3-C666-4E63-BFDB-52BCF557F938", etc.

Or as an alternative: Is there a way to store the headers of an exchange to a file to which later on the headers can be restored in another exchange?

Thank you.


EDIT: My fork of the example.

    public void jsonToHeaders(String body, @Headers Map<String, String> headers) throws ParseException {

        LOG.info("Starting JSON conversion...");
        LOG.debug("Body input, content: {} ", body);
        JSONParser parser = new JSONParser();
        JSONObject jsonObject = (JSONObject) parser.parse(body);
        if (jsonObject != null) 
        { 
            String stringValue = null;
            String stringKey = null ;
            final String NA_STRING = "*** N/A ***";

            for (Object key : jsonObject.keySet()) {

                stringKey = ((key == null) ? NA_STRING : (String)key);
                stringValue = ((jsonObject.get(stringKey) == null) ? NA_STRING : jsonObject.get(stringKey).toString());
                headers.put(stringKey, stringValue);
                LOG.debug("Processing key {} with value {}", stringKey, stringValue);
            }
            LOG.info("Done processed JSON: {}", headers.toString());
        }
    }
DevelopperX
  • 85
  • 1
  • 2
  • 9

2 Answers2

2

You can use bean for this case.

JSONToHeadersBean


package org.mybean;


import org.apache.camel.Headers;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class JSONToHeadersBean {

    public void jsonToHeaders(String body, @Headers Map<String, String> headers) throws ParseException {
        JSONParser parser = new JSONParser();
        JSONObject object = (JSONObject) parser.parse(body);
        object.keySet().forEach(key -> headers.put(key.toString(), object.get(key).toString()));
    }

    //for test
    public static void main(String[] args) throws ParseException {
        String body = "{\"msgId\": \"8600C5A3-C666-4E63-BFDB-52BCF557F938\", \"jiraId\": \"ERR002\"}";
        JSONParser parser = new JSONParser();
        JSONObject object = (JSONObject) parser.parse(body);
        final Map<String, String> headers = new HashMap<String, String>();
        object.keySet().forEach(key -> headers.put(key.toString(), object.get(key).toString()));
        System.out.println();
    }
}

Create bean

<bean class="org.mybean.JSONToHeadersBean" id="JSONToHeadersBean" name="JSONToHeadersBean"/>

And you can use it in route

<bean method="jsonToHeaders" ref="JSONToHeadersBean"/>
Lukyanov Mikhail
  • 500
  • 7
  • 17
  • I originally was looking for the answer in Spring DSL, but, seeing this which is very elegantly and with the space to add more parse/conversion utils to the bean, it meets and prefers (even) the solution. I did add some NPE checks, first of all to the JSONObject, secondly to the iteration on the keys. In some cases the key was resulted in a null , which game me an NPE. My version added to the bottom of my question. – DevelopperX Mar 04 '20 at 12:19
2

As an alternative you may parse JSON to HashMap and put it into a header:

.unmarshal().json(JsonLibrary.Jackson, java.util.Map.class)
.setHeader("params", simple("body"))

(requires camel-jackson dependency)

To access the stored values:

.log(LoggingLevel.INFO, "MsgId: ${header.params[msgId]}")
Anton Goncharov
  • 363
  • 3
  • 11